我正在尝试在 jquery 中使用 ajaxStop 函数,但无法触发它,有什么想法吗?
我想做的是遍历每个 anchor 标记,然后更新其中的一些内容,从那里我想使用 ajaxstop 事件来触发脚本以根据更新重新组织 anchor
感谢您的帮助
jQuery(document).ready(function($) {
function updateUsers() {
$(".twitch_user").each(function(index, user) {
$.ajax({ url: "https://api.twitch.tv/kraken/streams/" + $(user).attr("id") + "?callback=?", success: function(d) {
if(d.stream) {
$(user).addClass("online");
$(user).removeClass("offline");
$(user).children(".viewers").text(d.stream.viewers + " viewers");
} else {
$(user).addClass("offline");
$(user).removeClass("online");
$(user).children(".viewers").text("0 viewers");
}
console.log(d);
}, dataType: "json"});
});
}
//$(document).ajaxStart(function() {
// console.log("Event fired!");
// updateUsers().delay(2000);
//})
$(document).ajaxStop(function() {
console.log("Event fired!");
// updateUsers().delay(2000);
});
updateUsers();
});
最佳答案
显然,在执行 JSONP 请求时全局处理程序被关闭,如 this ticket 中所述。 :
JSONP requests are not guaranteed to complete (because errors are not caught). jQuery 1.5 forces the global option to false in that case so that the internal ajax request counter is guaranteed to get back to zero at one point or another.
我不确定 JSONP 是否是您的意图,但是 URL 末尾的 ?callback=?
让 jQuery 如此处理它。
解决方案是设置以下内容:
jQuery.ajaxPrefilter(function( options ) {
options.global = true;
});
关于javascript - jquery.each 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14590367/