如何停止ajax loader

时间:2013-05-24 12:29:25

标签: javascript ajax jquery

我实施了这个:

$('#loader').hide()  // hide it initially
.ajaxStart(function() {
                        $('#loader').show();
                      })
.ajaxStop(function() {
                        $('#loader').hide();
                     });

当我使用ajax方法时,这非常有效。但是我希望能够在这种情况下禁用ajax加载器。

    setInterval(function(){
      //I need to disable loader in here
    $.ajax({
            type: "POST",
            url: "live_top_5.php",
            dataType: 'json',
            cache: false,
            success: function(response) 
            {
                               //do something
            }
            });     
},10000);

问题是我的加载程序覆盖全屏,我想为这个特定的ajax调用禁用它,因为我每10秒刷新一次这个内容。那可能吗?要为某个ajax调用禁用ajax加载程序吗?

2 个答案:

答案 0 :(得分:2)

您可以选择此项,请查看此附加说明:

  

如果在全局选项设置为的情况下调用$ .ajax()或$ .ajaxSetup()   false,.ajaxStart()方法不会触发。

http://api.jquery.com/ajaxStart/

编辑:在不应触发事件的调用上添加全局参数

   $.ajax({
            type: "POST",
            url: "live_top_5.php",
            dataType: 'json',
            cache: false,
            global: false,
            success: function(response) 
            {
                               //do something
            }
            });    

答案 1 :(得分:0)

使用此 -

   setInterval(function(){
  setTimeout((function(){ $('#loader').hide();}),200);
$.ajax({
        type: "POST",
        url: "live_top_5.php",
        dataType: 'json',
        cache: false,
        success: function(response) 
        {
                           //do something
        }
        });     
},10000);

或者您可以在初始化中使用该标志

var allowed = true;
$('#loader').hide()  // hide it initially
.ajaxStart(function() {
                  if(allowed)  $('#loader').show();
                  })
.ajaxStop(function() {
                    $('#loader').hide();
                 });

并在ajax中检查

  setInterval(function(){
  allowed = false;
   $.ajax({
        type: "POST",
        url: "live_top_5.php",
        dataType: 'json',
        cache: false,
        success: function(response) 
        {
                 allowed = true;          //do something
        }
        });     
   },10000);