2 ms·
For future reference it's very rare that setInterval is what you want to use (vs recursive setTimeout, like another reply suggested) Unfortunately JavaScript i
by brundolf 5y ago
For future reference it's very rare that setInterval is what you want to use (vs recursive setTimeout, like another reply suggested)
Unfortunately JavaScript is one of those languages that has lots of "never or rarely use this" cases
- codewithcheese 5y agoWhy is recursive setTimeout better?
- kuekacang 5y agoNot necessarily better. Usually you want a recurring task that start after the previous invocation finished. Recursive setTimeout is the straight forward choice, while setInterval may require you to cancel the interval, making you just reimplementing setTimeout. Scheduling task in setInterval will run the task every interval time set regardless the previous task has finihsed or not. This in turn will starve your hardware if the function could not finish before the next execution begin.
- ricardobeat 5y ago> Scheduling task in setInterval will run the task every interval time set regardless the previous task has finished or not. This in turn will starve your hardware if the function could not finish before the next execution begin. That only applies to async work performed inside the callback. Once the CPU is starved, or if the work performed inside the callback takes longer than the interval, then setInterval will start to skip.