简体   繁体   English

如何停止运行此setTimeout函数?

[英]How can I stop this setTimeout function from running?

I use the function below to check on the status of a JSON file. 我使用下面的函数来检查JSON文件的状态。 It runs every 8 seconds (using setTimeout) to check if the file has changed. 它每8秒运行一次(使用setTimeout)来检查文件是否已更改。 Once the JSON's status becomes 'success' I no longer want to keep calling the function. 一旦JSON的状态变为“成功”,我就不再想继续调用该函数了。 Can someone please show me how to do this? 有人可以告诉我该怎么做吗? I suspect it involves the use of clearTimeout, but I'm unsure how to implement this. 我怀疑它涉及使用clearTimeout,但我不确定如何实现它。

Cheers! 干杯!

  $(function() {
    var checkBookStatus = function() {
       var job_id = "#{@job.job_id}";
       var msg = $('.msg');
       var msgBuilding = $('#msg-building');
       var msgQueuing = $('#msg-in-queue');
       var msgSuccessful = $('#msg-successful-build');
       var msgError = $('#msg-error'); 
       $.ajax({ 
         url: '/jobs/'+job_id+'/status.json',

           datatype: 'JSON',
           success:function(data){
             if (data.status == "failure")  {
               msg.hide();          
               msgError.show();
             }
             else if (data.status == "#{Job.queue}")  {
            msg.hide();          
            msgQueuing.show();
         }
             else if (data.status == "#{Job.building}")  {
            msg.hide();          
            msgBuilding.show();
         }             
         else if (data.status == "#{Job.failure}")  {
             msg.hide();          
             msgError.show();
         }
         else if (data.status == "#{Job.success}")  {
             msg.hide();          
             msgSuccessful.show();

              }                    
           },       
       }).always(function () {
            setTimeout(checkBookStatus, 8000);
      });
    };    
   checkBookStatus();    
  });

t = setTimeout(checkBookStatus, 8000); when you decide to stop the timeout use this clearTimeout(t); 当你决定停止超时使用这个clearTimeout(t); .

use clearTimeout 使用clearTimeout

eg you defined : 例如你定义:

id = setTimeout(checkBookStatus, 8000);  

then you can remove this function by : 然后你可以通过以下方式删除此功能

clearTimeout(id)

Call clearTimeout with the value previously returned by setTimeout . 使用setTimeout先前返回的值调用clearTimeout This would give you something like: 这会给你一些类似的东西:

$(function() {
  var timeoutID;
  var checkBookStatus = function () {
      […]
      else if (data.status == "#{Job.success}")  {
          clearTimeout(timeoutID);
      […]
       }).always(function () {
        timeoutID = setTimeout(checkBookStatus, 8000);
      […]

When you use setTimeout , use like this: 使用setTimeout ,请使用如下:

var myTime = setTimeout(checkBookStatus, 8000);

to clear it just: 清除它只是:

clearTimeout(myTime);

Before your call of checkBookStatus() at the end, put another call: var interval = setInterval(checkBookStatus, 8000); 在最后调用checkBookStatus()之前,再调用一次: var interval = setInterval(checkBookStatus, 8000); . Then on success you can clearInterval(interval) . 然后在成功时你可以clearInterval(interval)

Do not use setTimeout for iteration. 不要使用setTimeout进行迭代。

A lot of answers are suggesting just to use clearTimeout() however, you are checking the status after the timeout has expired, there is no timeout to clear. 很多答案建议只使用clearTimeout()但是,您在超时过期后检查状态,没有超时要清除。 You need to not call setTimeout() in your always() function rather than to clear anything. 您不需要在always()函数中调用setTimeout()而不是清除任何内容。 So you could re-inspect the status inside your always() function I suppose, but your data object isn't in scope there. 所以你可以重新检查我想的always()函数里面的状态,但你的data对象不在那里。 It would be preferable to just use setInterval() outside of your checkBookStatus() function. 最好只在checkBookStatus()函数之外使用setInterval()

$(function() {
    var checkBookStatus = function() {
        var job_id = "#{@job.job_id}";
        var msg = $('.msg');
        var msgBuilding = $('#msg-building');
        var msgQueuing = $('#msg-in-queue');
        var msgSuccessful = $('#msg-successful-build');
        var msgError = $('#msg-error'); 
        $.ajax({ 
            url: '/jobs/'+job_id+'/status.json',
            datatype: 'JSON',
            success:function(data){
                if (data.status == "failure")  {
                    msg.hide();          
                    msgError.show();
                }
                else if (data.status == "#{Job.queue}")  {
                    msg.hide();          
                    msgQueuing.show();
                }
                else if (data.status == "#{Job.building}")  {
                    msg.hide();          
                    msgBuilding.show();
                }             
                else if (data.status == "#{Job.failure}")  {
                    msg.hide();          
                    msgError.show();
                }
                else if (data.status == "#{Job.success}")  {
                    msg.hide();          
                    msgSuccessful.show();
                    clearInterval(interval);
                }                    
            }       
        });
    };    
    var interval = setInterval(checkBookStatus, 8000);    
    checkBookStatus();
});

the following should work... 以下应该工作......

the setTimeout function return an instance of the setTimeout function. setTimeout函数返回setTimeout函数的一个实例。 Keep this value in a variable and pass it to the function clearTimeout when you want to prevent the event from firing again. 将此值保存在变量中,并在希望防止事件再次触发时将其传递给函数clearTimeout。

ie

    var t = setTimeout(1000, someFunction);
...

//after you no longer need the timeout to fire, call

clearTimeout(t);

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

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