How to skip 1st delay of setInterval function?

when we use setInterval, it is delayed for the specified time, and then runs the function specified in the 1st argument. What should I do if I do not want a delay for the first time?

I have one solution that calls a function initially, and then use setInterval, so there won't be a first call to delay. but this is not the right decision. Therefore, if someone has any other solution, then kindly let me know.

+4
source share
4 answers
setInterval(function(){ doFunction(); }, 15000); doFunction(); 

does it imediatelly :)

+3
source

read this post. Changing the SetInterval Interval During Startup

Peter Bailey has a good general function, however gnarf also has a nice example.

To do this, you will have to create your own timer. SetInterval does not allow you to change the delay of an interval during its execution. Using setTimeout in a callback loop should allow you to edit the time interval.

+1
source

Another way:

 (function run(){ // code here setTimeout(run,1000); }()) 
+1
source

Create a wrapper for this.

 function runPeriodically(func, miliseconds) { func(); return setInterval(func, miliseconds); } function onePiece() { /* */ } runPeriodically(onePiece, 100); 
0
source

All Articles