最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find out if setTimeout is running on the background - Stack Overflow

programmeradmin5浏览0评论

I am having a problem to stop the setTimeout function fired previously.

I found some relative questions and the giving solution is to put the time_out variable outside of the function to make it work. I tried but still not working.

   this.A = function(){
        if(time_out) clearTimeout(time_out);  
        // time_out is nothing here and this will return error
        
        time_out = setTimeout(function(){  }, 3000);
    }
    
    -------------------------------------------------

     this.time_out = 0;
     this.A = function(){
          if(this.time_out) clearTimeout(this.time_out);  
          //will run through, but the setTimeout setup previously will keep running...
            
         this.time_out = setTimeout(function(){  }, 3000);
     }

Update

I tired put the whole function into a element and call it out when reset

if(el.data('time_out')) clearTimeout(el.data('time_out'));

This works perfect

I am having a problem to stop the setTimeout function fired previously.

I found some relative questions and the giving solution is to put the time_out variable outside of the function to make it work. I tried but still not working.

   this.A = function(){
        if(time_out) clearTimeout(time_out);  
        // time_out is nothing here and this will return error
        
        time_out = setTimeout(function(){  }, 3000);
    }
    
    -------------------------------------------------

     this.time_out = 0;
     this.A = function(){
          if(this.time_out) clearTimeout(this.time_out);  
          //will run through, but the setTimeout setup previously will keep running...
            
         this.time_out = setTimeout(function(){  }, 3000);
     }

Update

I tired put the whole function into a element and call it out when reset

if(el.data('time_out')) clearTimeout(el.data('time_out'));

This works perfect

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 9, 2013 at 1:00 MicahMicah 4,5608 gold badges32 silver badges40 bronze badges 7
  • 2 You problem seems to be the context of this. – elclanrs Commented Jan 9, 2013 at 1:02
  • 1 You could try declaring time_out as a global variable – Alex W Commented Jan 9, 2013 at 1:04
  • @AlexW - Storing anything in a global variable is almost always bad advice. – Joseph Silber Commented Jan 9, 2013 at 1:05
  • I think we need more code to be able to tell what the problem is... – elclanrs Commented Jan 9, 2013 at 1:06
  • I also tried var time_out outside of this.A, will return undefined everytime running this.A – Micah Commented Jan 9, 2013 at 1:08
 |  Show 2 more ments

3 Answers 3

Reset to default 3

make sure you are in the proper scope.

var timerFun = function(){
     var me = this;
     me.timer= undefined;

     me.startTimeout = function(){
        me.timer = setTimeout(me.SetterFunc, 3000);
     };

     me.setterFunc = function(){
        alert('oh hai!');
        me.ClearTimer();
     };

     me.clearTimer = function(){
        if(me.timer!= undefined){
            me.clearTimeout(me.timer);
        }
     };
     return me;
    };

Create trigger functions for timeout

startTimeOut(){
started = 1;
setTimeout(yourFunction, 2000);
}

well then u can check if started=1 u can stop it and if started=0 means it is stopped

stopTimeOut(){
started = 0;
clearTimeout(yourFunction);
}

It's quite easy to stop a setTimeout.

var timeout = setTimeout(function(){
    myFunc();
}, 1000);

function myFunc(){
    //do something...
};

To clear the timeout

clearTimeout(timeout);
发布评论

评论列表(0)

  1. 暂无评论