我正在循環查看一個列表,併爲每個項目執行ajax調用,這會返回數據。我試圖在每個項目完成時再做一次回調。我這裏有一些代碼,不工作,但希望能做出什麼,我試圖做的意義:在循環中等待多個被緩存的對象
var dfdArray = [];
$.each(setting, function (i, item) {
$.when(getCompany(item.CompanyId)).done(function (result) {
var dfd = new $.Deferred();
dfdArray.push(dfd);
getData(result.company[0]).done(function (data) {
setting.Pipes = data.pipes;
setting.Deals = data.deals;
// Here is where I want to continue
// to the final callback when each is resolved
dfd.resolve();
});
});
});
$.when.apply($, dfdArray).done(function() {
// Do some other stuff
});
是否要一個接一個地或同時執行呼叫? –
@BenjaminGruenbaum與此同時,而是繼續在 – tqrecords
你是非常接近每個「設置」中有數據,雖然你有[解析構造(http://stackoverflow.com/questions/23803743/what-is-the-顯式承諾構造反模式和如何做我避免它/ 23803744#23803744) –