简体   繁体   English

如何在ajax请求中停止setTimeout

[英]How to stop setTimeout inside ajax request

It makes ajax calls with 5 seconds delay between each calls. 它进行ajax调用,每次调用之间有5秒的延迟。

How can I stop this setTimeout inside ajax using clearTimeout after 30 seconds? 30秒后,如何使用clearTimeout在ajax中停止此setTimeout?

$.ajax({
    url  : url,
    success : function(response){
         var Object = this;
         setTimeout(function(){     //How to stop this setTimeout
         $.ajax(Object);}, 5000)}   //using clearTimeout after 30 seconds?
          },
    });

First of all, what you are doing is called " polling " technique. 首先,您正在做的事情称为“ 轮询 ”技术。 And to do that "right way", better to use recursion function, instead of: 为了做到“正确”,最好使用递归函数,而不是:

var Object = this;
$.ajax(Object);

Is better to write it like: 最好这样写:

function pollData() {
    $.ajax({
        url  : url,
        success : function(response){
            setTimeout(function() {
                pollData();
            }, 5000);
        }
}

Next question is, what if you want to stop polling for some reason (after 3 seconds for example)? 下一个问题是,如果您由于某种原因想要停止轮询(例如3秒钟后),该怎么办? setTimeout returns an ID which you can use later to clearTimeout : setTimeout返回一个ID,您以后可以使用它clearTimeout

var pollingStatus = true;
var pollingTm = null;

function pollData() {
    $.ajax({
        url  : url,
        success : function(response){
            if (pollingStatus) {
                pollingTm = setTimeout(function() {
                    pollData();
                }, 5000);
            }
        }
}

function stopPolling() {
    pollingStatus = false;
    clearTimeout(pollingTm);
}

setTimeout(stopPolling, 3000);

You can set obj_timeout = setTimeout(... this way you will have refference to the object out of scope 您可以设置obj_timeout = setTimeout(...这样,您将引用超出范围的对象

var obj_timeout = null; 
var stop_sending_loop = false;

$.ajax({ 
  url  : url,
  success : function(response){
     var Object = this;

     obj_timeout = setTimeout(function(){     //How to stop this setTimeout
          if(!stop_sending_loop)
            $.ajax(Object);
         }, 5000)
       }   //using clearTimeout after 30 seconds?
     },
});

var stop_to = function(){
   if(obj_timeout != null)
   clearTimeout(obj_timeout)
   stop_sending_loop = true;
}

setTimeout(stop_to,30000);

see: https://www.w3schools.com/jsref/met_win_cleartimeout.asp 请参阅: https//www.w3schools.com/jsref/met_win_cleartimeout.asp

Declare a global counter variable as 0 outside success fuction. 将外部计数器函数的全局计数器变量声明为0。 For ex var counter = 0; 对于ex var counter = 0;

In success function, set your setTimeout as follows 在成功功能中,如下设置setTimeout

var myVar = setTimeout(function(){
//How to stop this setTimeout
     counter++;
     $.ajax(Object);}, 5000)}   //using clearTimeout after 30 seconds?
      },

if (counter == 6) { clearTimeout(myVar);} // Since, 30/5 = 6

clearTimeout will stop setTimeout. clearTimeout将停止setTimeout。 and counter ensures that clearTimeout is triggered after six calls. 并且计数器确保在六个调用之后触发clearTimeout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM