采用以下示例:
_.each(arrayOfVals, function (val) {
$.when(getAjaxCall('foo',{val:val}))
.then(function (callResponse) {
_.each(callResponse, function (rep) {
console.log(rep);
});
});
然后我希望在完整代码完成后调用一些代码。我怎么能这样做?
答案 0 :(得分:2)
您可以将多个参数传递给$.when
,并且当它们全部完成时,它的延迟将解析。如果您有阵列,请使用.apply
。
$.when.apply($, arrayOfVals.map(val => getAjaxCall('foo', {val}))
.then(responses => responses.map(resp => console.log(resp));
答案 1 :(得分:0)
我最终使用$ .Deferred对象来监听所有内容何时完成,并在我浏览整个列表时调用解析。
deferred = $.Deferred
i = 0;
_.each(arrayOfVals, function (val) {
$.when(getAjaxCall('foo',{val:val}))
.then(function (callResponse) {
_.each(callResponse, function (rep) {
i += 1;
console.log(rep);
if (i == callResponse.length) {
deferred.resolve();
}
});
});
deferred.done(function() {
console.log('everything done!');
}