jQuery ajaxStart,ajaxStop尴尬行为

时间:2011-06-12 23:14:40

标签: javascript jquery ajax google-chrome fade

我这是为了说明我的问题: http://jsfiddle.net/michaelhart/mUMHZ/

(仅在Chrome中测试过。)

总结:当用户退出并返回时,我不希望ajax活动指示器发疯。

我试过

$("#notification").clearQueue();
$("#notification").hide();

$("#notification").clearQueue();
$("#notification").show();

$('#notification').fadeOut();

但这只会使标签在标签输出后完全停止运行。

我不确定这是否是谷歌Chrome处理非活动标签的方式中的错误,或者是当Chrome将其置于“睡眠状态”时jQuery的工作方式。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

jQuery bug跟踪器中的bug is noted。建议的修复是,而不是盲目地使用setInterval,使用动画的回调函数来触发setTimeout重启。

E.g。

$(document).ready(function() {
    fetchAjax();
});

function fetchAjax() {
        var randomnumber=Math.floor(Math.random()*10001);
        $('#number').html(randomnumber);
        $.ajax({
            type: 'POST',
            url: '/echo/html/',
            data: {
              'html': randomnumber
            },
            dataType: 'text/html'
        });
}

$(document).ajaxStart(function(){
    $('#notification').fadeIn();
}).ajaxStop(function(){
    $('#notification').fadeOut(function(){
      setTimeout(function(){ fetchAjax(); }, 5*1000);
    });
});