我已经提到了以下链接
Multiple ajax calls inside a each() function.. then do something once ALL of them are finished?
Coordinating multiple ajax requests with jquery.when
我想在foreach循环中使用jquery.when方法。所以想要了解以下代码中的参数
$.when.apply($, calls).then(function(call1, call2, call3) {
});
假设var calls = [];
是3个get ajax调用的数组。所以在上面是有必要提供3个参数,如call1, call2, call3
,或者所有这3个调用可以组合成一个参数call
,如下所示?
$.when.apply($, calls).then(function(call) {
});
答案 0 :(得分:0)
const requests = [
$.get("test.json").then(function() { return "get"; }),
$.getJSON("test.json").then(function() { return "getJSON"; }),
$.ajax({ url: "test.json", method: "get", dataType: "JSON"}).then(function() { return "ajax"; })
]
$.when.apply($, requests)
.then(function() {
for (var i = 0, l = arguments.length; i < l; i++) {
document.body.insertAdjacentHTML("beforeend", "<p>" + arguments[i] + "</p>");
}
});
如果将Deferreds的数量传递给jQuery.when()是动态的,则应按照上面的示例传递doneCallbacks的参数。感谢@Andreas的宝贵指导