Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a timer is cleared or timed out in javascript?

Ok, really simple question. I'm taking a crash course in javascript.

If I use timer = setTimeout(..., 500) to set a timer, and then clearTimeout(timer) to clear the timer, the integer value of timer doesn't change, so my question is how to know if a timer is timed out or cleared?

I want to use if (timer) {...} , but obviously a positive integer always returns true.

like image 740
lai Avatar asked Oct 26 '10 07:10

lai


People also ask

What is clear timeout in JavaScript?

The clearTimeout() function in javascript clears the timeout which has been set by setTimeout()function before that. setTimeout() function takes two parameters. First a function to be executed and second after how much time (in ms). setTimeout() executes the passed function after given time.

Should setTimeout be cleared?

No, setTimeout stops running after 1 call. In order to stop setInterval however, you must use clearInterval . If you create an endless loop of setTimeout then clearTimeout could be used.

Can we stop setTimeout in JavaScript?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.

How do I get rid of timeout?

To remove Timeouts on mobile, Press and hold on the User's avatar to bring up their profile. Then press Remove Timeout and confirm the removal.


2 Answers

If you're looking for something more formal, you could build javascript class that encapsulates the setTimeout/clearTimeout functionality.

Such a class might look something like this:

/** class Timer **/
var Timer = function(delayMs, callbackFunc) {
    this.delayMs = delayMs;
    this.callbackFunc = callbackFunc;
    this.timerState = 'new';
}
Timer.prototype.start = function() {
    if( this.tmr ) return;

    var self = this;
    this.timerState = 'running';
    this.tmr = setTimeout(function() { self._handleTmr(); }, this.delayMs);
}
Timer.prototype.cancel = function() {
    if( ! this.tmr ) return;

    clearTimeout(this.tmr);
    this.tmr = null;
    this.timerState = 'canceled';
}
Timer.prototype._handleTmr = function() {
    this.tmr = null;
    this.timerState = 'completed';
    this.callbackFunc();
}

I've also included a timerState attribute that would let you easily determine whether the timer was "completed" or "canceled".

You would use it like this:

var t = new Timer(500, function() {
    alert('timer completed');
});
t.start();

// do whatever...

// now cancel the timer if it hasn't completed yet.
t.cancel();

// maybe you do some other stuff...
// then check the timerState, and act accordingly.
//
if( t.timerState == 'canceled' ) {
   alert("the timer was canceled!");
} else {
   alert("the timer completed uneventfully.");
}

You can extend the same basic idea to include additional functionality if you need it (eg. repeating timer, start/stop/resume, etc.)

like image 105
Lee Avatar answered Sep 30 '22 02:09

Lee


assign null to the timer after the clearTimeout(timer)

like image 38
Alex Gyoshev Avatar answered Sep 30 '22 01:09

Alex Gyoshev