gpt4 book ai didi

ajax - jquery 多个ajax 检查是否全部完成? (顺序不重要)

转载 作者:行者123 更新时间:2023-11-30 23:56:45 25 4
gpt4 key购买 nike

有没有一种巧妙的方法来确保一堆ajax回调全部完成?它们不需要按顺序执行,我只需要所有数据都在那里。

一个想法是让它们在完成时增加一个计数器并检查 counter == countMax 是否,但这看起来很难看。另外,是否存在同步问题? (从同时读/写到计数器)

最佳答案

.ajaxStop()方法可用于此,它仅在当前 ajax 请求的最后一个完成时触发。完整描述如下:

Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop event. Any and all handlers that have been registered with the .ajaxStop() method are executed at this time.

例如,如果您只想运行某些内容,文档是一个很好的 Hook 位置:

$(document).ajaxStop(function() {
//all requests finished!, do something with all your new fancy data
});

或者您可以在发生这种情况时显示一条消息,如下所示:

$("#ajaxProgressDiv").ajaxStop(function() {
//display for 2 seconds then fade out
$(this).text("All done!").delay(2000).fadeOut();
});

关于ajax - jquery 多个ajax 检查是否全部完成? (顺序不重要),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153712/

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