weixin_33725239 2013-01-29 19:06 采纳率: 0%
浏览 573

jQuery .each同步

I'm trying to use the ajaxStop function in jquery but can't get it to fire, any ideas?

What I'm trying to do is loop through each anchor tag and then update some content inside it, from there I want to use the ajaxstop event to fire a script to reorganize the anchors based on the updates

Thanks for any help

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();
});
  • 写回答

1条回答 默认 最新

  • weixin_33743248 2013-01-29 19:31
    关注

    Apparently the global handlers are turned off when doing JSONP requests, as explained in 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.

    I'm not sure if JSONP is your intention or not, but the ?callback=? on the end of the URL makes jQuery handle it as such.

    The solution was to set the following:

    jQuery.ajaxPrefilter(function( options ) {
        options.global = true;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog