jQuery$.每个嵌套在$.When中
jQuery $.each nested in $.When
我有一个iten列表要通过一个返回$.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));
$.each
并没有像您的代码所期望的那样返回一个promise数组。您应该先构建一个promise数组,然后用您的数组调用$.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)
});
相关文章:
- jQuery.when()在循环中被忽略
- 为什么有些jQuery程序员使用像$.get、$.getJSON和$.when这样的jQuery函数来代替$.ajax
- Jquery:$.when根据参数的数量表现不同
- jQuery.when() 需要清除
- Jquery动画和when函数
- 使用 jquery.when 和 done 将值从一个 ajax 调用传递到另一个
- 如何在使用jQuery$.when执行多个请求时将响应分配给请求
- 我可以将Promises传递给jQuery.when(),还是只传递Deferred
- jQuery.when的后期绑定
- 使用jQuery.when时进行了奇怪的变量初始化
- .when 在 jquery 中出现问题
- jQuery 使用 .when 并推送数组
- jQuery.when, 无法获得 gata.
- jQuery.when 函数,在 ajax 调用完成后无法获取值
- 如何与 JQuery WHEN 和 THEN 同步两个骨干获取调用
- jQuery $.when(..).完成(..)未按预期工作
- 如何使用 jquery .when 访问 json 数据以附加到页面
- jQuery: $.when 延迟的 AJAX 调用 — 一个失败的调用会取消其他调用
- 有没有更简单的方法可以为不同的选择器链接不同的 jQuery 操作,然后嵌套 $.when
- Callback event when jQuery 'mmenu' closed