我有一个触发 3 个 ajax 调用的点击事件:
$("#button").click(function(e) {
e.preventDefault();
$.ajax(call1);
$.ajax(call2);
$.ajax(call3);
some_function() //should fire off AFTER all ajax calls are complete
});
在触发我的 some_function()
函数之前,有没有办法让我确认所有 ajax 调用都已触发?
提前致谢!
最佳答案
您可以使用 $.when
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
$.when($.ajax(call1), $.ajax(call2), $.ajax(call3))
.done(function () {
some_function();
});
如果(出于某种原因)您有一组 promise ,那么您可以使用 Function.prototype.apply() 调用此方法:
$.when.apply($, ajaxArray)
.done(function () {
some_function();
});
关于javascript - 所有 ajax 调用完成后的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905871/