gpt4 book ai didi

javascript - jQuery $.each 嵌套在 $.When

转载 作者:行者123 更新时间:2023-11-30 08:44:44 28 4
gpt4 key购买 nike

我有一个要传递给返回 $.ajax() 的函数的 itens 列表,但是 .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)
});

关于javascript - jQuery $.each 嵌套在 $.When,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22991453/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com