Change interval setInterval

Is there a way to change the interval of calls to a set of functions with setInterval at run time, other than deleting it (clearInterval) and restoring it with a different value?

+4
source share
2 answers

Use setTimeout instead, and it is also a non-blocking method for async JS:

var interval = 1000; function callback() { console.log( 'callback!' ); interval -= 100; // actually this will kill your browser when goes to 0, but shows the idea setTimeout( callback, interval ); } setTimeout( callback, interval ); 

Do not use setInterval , as in some cases (many setInterval + long callbacks, which are usually longer than the timeout) due to the limited queue size, some callbacks will be deleted by the browser and never executed . Only setTimeout guarantees execution.

+14
source

Nope; deleting the interval and re-adding is the way to do this if you used setInterval() .

You can perform the same task with a variable timeout, however, by re-typing setTimeout() with a variable delay at the end.

Out of curiosity, what are you doing to change the interval? Perhaps requestAnimationFrame() might be more appropriate?

+2
source

All Articles