Jquery 等待$.ajax完成而不使用$.when

Jquery 等待$.ajax完成而不使用$.when,jquery,Jquery,在我的项目中,我必须使用jquery1.4.x。我有四个异步$.ajax函数启动,所有函数都返回true/false。我需要等待在IF中对它们进行评估,因为如果我现在进行评估,并不是所有的射击都完成了 代码(全部为$(文档)。就绪..:` function getHTTPStatusCode200(urlIn) { $.ajax({ url: urlIn, complete: function (xhr) {

在我的项目中,我必须使用jquery1.4.x。我有四个异步$.ajax函数启动,所有函数都返回true/false。我需要等待在IF中对它们进行评估,因为如果我现在进行评估,并不是所有的射击都完成了

代码(全部为$(文档)。就绪..:`

    function getHTTPStatusCode200(urlIn) {
        $.ajax({
            url: urlIn,
            complete: function (xhr) {
                return (xhr.status == 200);
            }
        });
    }
其中有四个,我需要这样做:

if(getHTTPStatusCode200(url) && getHTTPStatusCode401(url)){
     alert("It worked.");
}
    $.ajax({
        url: urlIn,
        async: false,
        complete: function (xhr) {
            return (xhr.status == 200);
        }
    });

`

添加
async:false
,如下所示:

if(getHTTPStatusCode200(url) && getHTTPStatusCode401(url)){
     alert("It worked.");
}
    $.ajax({
        url: urlIn,
        async: false,
        complete: function (xhr) {
            return (xhr.status == 200);
        }
    });

没有延迟对象并不意味着不能使用回调

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

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

$。当
概念不难模仿时:

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

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

定义方法,使其返回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 = 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);

现在已经过测试,可以正常工作。

在完整的处理程序中如何,似乎是一个很好的地方来坚持这种情况。既然您是jQuery的老版本,我建议您在每个ajax调用中使用
async:false
,如果您按顺序运行它们。请注意
async
将阻止您的浏览器,直到
ajax
关闭这正是
时的用途。为什么这不是一个选项?它不是在1.4中存在吗?@ogc nick beacuse OPs使用v1.4。
在v.1.5中引入
时,请查看下面的答案。我提供了一个完整的替换方法来实现
$。在
时。您仍然无法从这样的回调中返回,并且异步:false是一种不好的做法。这是一种方法……。如果你试图锁定浏览器并骚扰用户。你们正在假设一个使用量很大的应用程序,使用(相对而言)每个ajax调用的处理时间都很长。OP是最好的判断。在我的项目中,有几个
async:false
是有帮助的,并且不会以任何方式影响用户体验,即使使用秒表也是如此。每个项目都是不同的。除非你有一个高延迟的连接,无论出于什么原因。它是ut在这种情况下没有必要。只有在
async:false
的情况下才是一个好主意,那就是在
onbeforeunload
回调中。请注意,正如@KevinB所说,您无论如何都不能返回。好的,伙计们,谢谢你们的评论。我感谢你们的更正。