jQuery:等待多个GET请求成功处理

jQuery:等待多个GET请求成功处理,jquery,wait,get-request,Jquery,Wait,Get Request,我需要发出多个$.get请求,处理它们的响应,并在同一数组中记录处理结果。代码如下所示: $.get("http://mysite1", function(response){ results[x1] = y1; } $.get("http://mysite2", function(response){ results[x2] = y2; } // rest of the code, e.g., print results var d1 = $.get(...); var d2 =

我需要发出多个$.get请求,处理它们的响应,并在同一数组中记录处理结果。代码如下所示:

$.get("http://mysite1", function(response){
  results[x1] = y1;
}
$.get("http://mysite2", function(response){
  results[x2] = y2;
}

// rest of the code, e.g., print results
var d1 = $.get(...);
var d2 = $.get(...);

$.when(d1, d2).done(function() {
    // ...do stuff...
});

在我继续我的代码的其余部分之前,是否要确保所有成功函数都已完成?

有一个非常优雅的解决方案:jQuery延迟对象。$。get返回一个实现延迟接口的jqXHR对象-这些对象可以如下组合:

$.get("http://mysite1", function(response){
  results[x1] = y1;
}
$.get("http://mysite2", function(response){
  results[x2] = y2;
}

// rest of the code, e.g., print results
var d1 = $.get(...);
var d2 = $.get(...);

$.when(d1, d2).done(function() {
    // ...do stuff...
});
阅读更多信息,并使用组合多个
延迟的
s(这就是
$.ajax
返回的内容)


如果在$.get调用中有success函数,该解决方案是否仍然有效?我以前试过,运气不好!在$.when()内使用$.get().done()似乎不再有效