【问题标题】:stop a jQuery function [duplicate]停止一个jQuery函数[重复]
【发布时间】:2013-07-03 12:49:24
【问题描述】:

使用 setTimeout 每 5 秒执行一次 jQuery 函数,如何通过单击 div 来停止该过程?

我的代码是:

function crono(selection){
    $.ajax({
        type: 'GET', 
        url: 'my_page.php', 
        data: {
            my_page: selection.attr('id')
        },
        success: function(data, textStatus, jqXHR) {
            selection.html(data);   
        }
    });
}


function repeat(){
    setTimeout(function() {
        $('.toReload').each(function(){
            crono($(this));
        });
        repeat();
    }, 5000);
}

repeat();

【问题讨论】:

  • 你想在什么情况下停止它?只需调用clearTimeout(var),其中var 是要清除的计时器。
  • 使用命名的超时并在您想取消它时调用clearTimeout

标签: jquery function settimeout


【解决方案1】:

使用clearTimeouthttps://developer.mozilla.org/fr/docs/DOM/window.clearTimeout

var handle = null;

function repeat(){
    handle  = setTimeout(function() {
        $('.toReload').each(function(){
            crono($(this));
        });
        repeat();
    }, 5000);
}

并通过调用取消setTimeout

clearTimeout(handle);

【讨论】:

  • 完美运行,非常感谢
  • 无法停止 ajax 请求。
【解决方案2】:

使用标志来做到这一点。

var end = false;

function repeat(){
   setTimeout(function() {
      if(!end){ 
          $('.toReload').each(function(){
            crono($(this));
          });
          repeat();
      }     
   }, 5000);
}
...

$('#div').click(function(){
 end = true;
});

【讨论】:

    【解决方案3】:

    使用clearTimeout 函数

    var timeout;
    
    function repeat(){
        timeout = setTimeout(function() {
            $('.toReload').each(function(){
                crono($(this));
            });
            repeat();
        }, 5000);
    }
    

    并在 div 上单击调用以下函数

    function stopAjax(){
        clearTimeout(timeout);
    }
    

    【讨论】:

      【解决方案4】:

      你需要明确的超时和ajax请求:

          var getData = {
          handleTimeout : 0,
          handleAjax,
      
          crono : function(selection){
              $.ajax({
                  type: 'GET', 
                  url: 'my_page.php', 
                  data: {
                      my_page: selection.attr('id')
                  },
                  success: function(data, textStatus, jqXHR) {
                      selection.html(data);   
                  }
              });
          },
      
          repeat: function(){
              this = ob;
              this.handleTimeout = setTimeout(function() {
                  $('.toReload').each(function(){
                      ob.crono($(this));
                  });
                  repeat();
              }, 5000);
          },
      
          stopAll: function(){
              clearTimeout(this.handleInterval);
              this.handleAjax.abort();
          }
      }
      
      getData.repeat();
      getData.stopAll();
      

      【讨论】:

        【解决方案5】:

        使用超时 ID。

        var timeoutID = 0;
        function repeat(){
             timeoutID = setTimeout(function() {
                $('.toReload').each(function(){
                    crono($(this));
                });
                repeat();
            }, 5000);
        }
        
        repeat();
        
        //stop the time out
        clearTimeout(timeoutID);
        

        【讨论】:

        • 您在函数中将 timeoutID var 重新定义为 loval var。它不会工作
        • 感谢您的评论。已编辑
        • 对不起,恰恰相反:var 声明必须在函数外部,而不是内部。
        • 还在睡觉。上午 9 点在这里大声笑
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多