Leo*_*rdo 1 javascript ajax jquery
我有一个itens列表来传递一个返回a的函数,$.ajax()但是在.done调用之后$.each调用它,而不是在内部ajax调用完成之后!我该怎么办?!?
这是我的代码:
$.when(
$.each(data, function (index, item) {
GetReservation(
item.UniqueId,
apiRoot,
//this is the 'success' function
function (data2) {
//do my stuff
},
//this is the 'error' function
function (x, y, z) {
if (x.status == 404) {
//OK, no reservations!
}
});
})
).done(setButtons(box, c));
Run Code Online (Sandbox Code Playgroud)
$.each并没有像你的代码似乎期待的那样返回一系列的promises.你应该首先构建一个promises数组,然后调用$.when你的数组:
var promises = [];
$.each(data, function (index, item) {
// add promise to array
promises.push(GetReservation(
item.UniqueId,
apiRoot,
//this is the 'success' function
function (data2) {
//do my stuff
},
//this is the 'error' function
function (x, y, z) {
if (x.status == 404) {
//OK, no reservations!
}
}
));
})
$.when.apply($, promises).done(function(){
setButtons(box, c)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |