[英]Suspend execution of AJAX callback after multiple AJAX calls
I have 2 Ajax calls in my script, Each one of them has a different callback to execute on call success.我的脚本中有 2 个 Ajax 调用,每个调用都有一个不同的回调来在调用成功时执行。
$.ajax({
type: 'POST',
url: "url1",
success: foo1
});
$.ajax({
type: 'POST',
url: "url2",
success: foo2
});
I have another function, Clear()
, that i would like to activate once all the ajax calls returned, but I do not want that the callbacks ( foo1
and foo2
) will be executed until I execute Clear()
.我有另一个函数Clear()
,我想在所有 ajax 调用返回后激活它,但我不希望回调( foo1
和foo2
)在我执行Clear()
之前执行。
I have searched for an answer but did not found clear one.我已经搜索了一个答案,但没有找到明确的答案。 What can |I do to solve this?我能做些什么来解决这个问题? is there any flag or function that deals in those kind of problems?是否有任何标志或功能可以处理此类问题?
Thanks in advance.提前致谢。
I'm not sure I agree with any of these existing solutions, as JQuery gives us a tool for just this purpose: $.when
我不确定我是否同意这些现有解决方案中的任何一个,因为 JQuery 为我们提供了一个专门用于此目的的工具: $.when
https://api.jquery.com/jquery.when/ https://api.jquery.com/jquery.when/
When using $.when
, JQuery aggregates all the promises into a single promise, and only resolves when all promises have resolved.使用$.when
,JQuery 将所有承诺聚合到一个承诺中,并且只有在所有承诺都已解决时才会解决。 If any promises are rejected, the fail
is called.如果任何承诺被拒绝,则调用fail
。
const promise1 = $.Deferred();
const promise2 = $.Deferred();
promise1.resolve({data: 'Promise 1'});
promise2.resolve({data: 'Promise 2'});
$.when.apply(this, [promise1, promise2])
.done((promise1Data, promise2Data) => {
// All of our promises have resolved
// And we have all our data from each promise
// Do the work of clear() here
console.log(promise1Data.data)
console.log(promise2Data.data)
})
.fail((error) => {
console.error('One or more promises failed')
});
As we can see, in the context of this question, you can do whatever logic is necessary inside of the done
of the $.when
.正如我们所见,在这个问题的上下文中,您可以在$.when
的done
内部执行任何必要的逻辑。 If you reach this point, you're guaranteed that all promises have resolved.如果您到达这一点,则可以保证所有承诺都已解决。
The above code block outputs the following to console上面的代码块将以下内容输出到控制台
> "Promise 1"
> "Promise 2"
Working example: http://codepen.io/mikechabot/pen/MyrJYB?editors=0011工作示例: http : //codepen.io/mikechabot/pen/MyrJYB?editors=0011
You can implement a naive but easy solution as below, or otherwise take a look at JS Promises if you need a stronger solution.您可以实现一个简单但简单的解决方案,如下所示,或者如果您需要更强大的解决方案,请查看JS Promises 。
var foo1Data, foo2Data;
$.ajax({
type: 'POST',
url: "url1",
success: function(data) {
if (foo2Data) {
Clear();
foo1(data);
foo2(foo2Data);
}
else {
foo1Data = data;
}
}
});
$.ajax({
type: 'POST',
url: "url2",
success: function(data) {
if (foo1Data) {
Clear();
foo1(foo1Data);
foo2(data);
}
else {
foo2Data = data;
}
}
});
Untested, but you could try something like this using when
.未经测试,但您可以使用when
尝试类似的操作。
// each object in responses contains the response text,
// status, and jqXHR object for each ajax call
function clear(responses) {
// do something
foo1(responses[0]);
foo2(responses[1]);
};
$.when(ajax1, ajax2).done(clear);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.