等到$ .ajax完成而不使用$ .when

时间:2013-06-20 18:28:18

标签: jquery

在我的项目中,我必须使用jQuery 1.4.x.我有四个异步$ .ajax函数触发,并且都返回true / false。我需要等待在IF中评估它们,因为如果我现在这样做,并非所有人都完成了射击。

代码(全部在$(文件).ready ..:`

    function getHTTPStatusCode200(urlIn) {
        $.ajax({
            url: urlIn,
            complete: function (xhr) {
                return (xhr.status == 200);
            }
        });
    }

其中有四个,我需要做这样的事情:

if(getHTTPStatusCode200(url) && getHTTPStatusCode401(url)){
     alert("It worked.");
}

`

4 个答案:

答案 0 :(得分:2)

$.when概念并不难模仿:

var results = [];
function newResult(result) {
    results.push(result);
    if (results.length === 4) {
        alert("It worked.");
    }
}

function getHTTPStatusCode200(urlIn) {
    $.ajax({
        url: urlIn,
        complete: function (xhr) {
            if (xhr.status === 200) {
                newResult(xhr);
            }
        }
    });
}

// your other functions, each calling newResult when done

答案 1 :(得分:1)

仅仅因为你没有延迟对象并不意味着你不能使用回调。

function getHTTPStatusCode200(urlIn,callback) {
    $.ajax({
        url: urlIn,
        complete: function (xhr) {
            callback(xhr.status)
        }
    });
}

getHTTPStatusCode200("foo.php",function(status){
    alert(status);
});

答案 2 :(得分:0)

非阻塞解决方案同样快速地通过成功回调异步调用它们,但是顺序调用它们:

定义方法,使其返回ajax对象。

function getHTTPStatusCode200(urlIn) {
    return $.ajax({
        url: urlIn
    });
}

然后链接他们:

getHTTPStatusCode200(url1).complete(function(xhr1){
    getHTTPStatusCode200(url2).complete(function(xhr2){
        getHTTPStatusCode200(url3).complete(function(xhr3){
            getHTTPStatusCode200(url4).complete(function(xhr4){
                //all requests have been completed and you have access to all responses
            }
        }
    }
}

将此作为$ .when:

的直接替代品
$.when = function() {
    this.requests = [];
    this.completeFunc;
    this.interval;

    //if you want it to work jsut like $.when use this
    this.requests = arguments;
    //If you want to pass in URLs use this
    //for(var i in arguments) {
    //    this.requests.push($.get(arguments[i]));
    //};

    this.complete = function(func) {
        this.completeFunc = func;
        var when = this;
        this.interval = window.setInterval(function(){
            checkReadyStates(when)
        }, 100);
        return this;
    };

    var checkReadyStates = function(when) {
        var complete = 0;
        for(var i in when.requests) {
            if(when.requests[i].readyState==4) {
                complete++;
            }
        }
        if(complete == when.requests.length) {
            window.clearInterval(when.interval);
            when.completeFunc.apply(when, when.requests);
        }
    };

    return this;
}

function handler(){ 
    console.log('handler');
    console.log(arguments);
    //The arguments here are all of the completed ajax requests
    //see http://api.jquery.com/jQuery.ajax/#jqXHR
}

$.when(xhr1,xhr2,xhr3).complete(handler);
//or
//$.when('index.php','index2.php','index3.php').complete(handler);

现在测试并且有效。

答案 3 :(得分:-1)

添加async:false,如下所示:

    $.ajax({
        url: urlIn,
        async: false,
        complete: function (xhr) {
            return (xhr.status == 200);
        }
    });