Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change interval of setInterval [duplicate]

Tags:

javascript

Is there a way of modifying the interval of calls of function set with setInterval during runtime, other than removing it (clearInterval) and reinstating again with a different value?

like image 568
SF. Avatar asked Dec 05 '22 12:12

SF.


1 Answers

Use setTimeout instead, additionally this 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 );

Don't use setInterval, as in some cases (lots of setInterval + long callbacks, which are usually longer than timeout), due to limited queue size, some callbacks will be dropped by the browser and never executed. Only setTimeout guarantees execution.

like image 193
oleq Avatar answered Dec 26 '22 08:12

oleq