Using setInterval/ setTimeout, how can I make sure my function FINISH executing before waiting for some time and then executing again, FINISH, then wait, and so on. Thanks.
Using setInterval/ setTimeout, how can I make sure my function FINISH executing before waiting
Share
That’s the classic use case for a chained series of
setTimeout:Since
setTimeoutonly schedules a single call to the function, you reschedule it (if appropriate) after the function has done its work.Unlike
setInterval, this works correctly even if the work your function does is asynchronous, as long as you reschedule it from the callback of the asynchronous work. For example:In contrast, if you’re doing something asynchronous, using
setIntervalrapidly becomes chaotic.