1

I have a flurry of interrupts coming into a handler and I don't want to service them until 5ms have passed since the last interrupt, indicating the flurry is over.

My thought was to call setTimeout(LookAtInterrupts, 5) each time the handler is entered, but I don't see in the setTimeout() documentation that it will cancel a pending scheduled call if it's called before that execution occurs.

Is that, in fact, what it will do? I don't want to get called 5ms after every handler interrupt, just the last one.

7 Answers 7

9

No, it won't - you can have as many pending timeouts as you want.

The function returns a key that you can use to cancel a timeout later:

var key = setTimeout( ... );

Then to cancel it later:

clearTimeout(key);

Calling clearTimeout() with an already-expired key is not an error, so you don't have to worry about synchronization problems.

2

setTimeout will not reset itself.

You can reset a timeout manually by

var timeout = setTimeout(...);
clearTimeout(timeout);
1

A setTimeout() won't cancel any previous timeouts implicitly.

However, you can achieve that by storing the identifier in a variable and clearing that each time.

var timeoutId = null;
var yourFn = function() { 
     clearTimeout(timeoutId);
     timeoutId = setTimeout(fn, 5);
};
1

You need to store a reference. setTimeout result can be stored and cleared later on.

For a "resettable" setTimeout:

// first assign it
var timeout = setTimeout(function(){
  foo();
}, 50);

// then look for an existing assignment before re-assinging
if (timeout) clearTimeout(timeout);
timeout = setTimeout(function(){
  bar();
}, 50);

References:

As an aside, be careful when setting a timeout < 5ms. Though HTML5 is supposed to support 4, I doubt you're actually getting anywhere close to that (w/ cost of spinning up the timeout).

0

Store reference to that setTimeout call in a variable, and after each successfull interrupt, before creating timeout, cancel the previous timeout,

var tv = null;

function interrupt(){
    if(tv !== null){
        clearTimeout(tv);
    }
    tv = setTimeout(LookAtInterrupts,5)
}

function LookAtInterrupts(){

}

By this, you'll guarantee that only the last interrupt will continue execution in 5ms intervals. I hope that was clear.

0

While you can implement this yourself, a more practical solution would be to grab underscore.js and use it's debounce function (see http://underscorejs.org/#debounce).

Then you can do:

var lookAtInterrupts = _.debounce(handleInterrupt, 5);

the resulting function will only run at most once every 5 ms.

0

When setTimeout() is executed it schedules one call to your binded function().

If you want to cancel it you have to get ID returned by setTimeout() and clear as:

var timeOutID = setTimeout( LookAtInterrupts, 5 );
(...)
clearTimeOut( timeOutID );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.