How can i return a value from a callback function to the caller?

前端 未结 3 2020
[愿得一人]
[愿得一人] 2021-01-22 16:22

How can i make this little function \"imageExists\" return wether the ajax request was successful or not?

function imageExists(path){
    $.ajax({
        url: p         


        
3条回答
  •  执念已碎
    2021-01-22 16:53

    I believe you'll have to use synchronous mode and use a separate a variable for storing the return value.

    function imageExists(path){
        var isSuccess;
        $.ajax({
            url: path,
            type: 'HEAD',
            async: false,
            error:
                function(){
                    isSuccess = false;
                    return false;
                },
            success:
                function(){
                    isSuccess = true;
                    return true;
                }
        });
        return isSuccess;
    }

提交回复
热议问题