3 ms·
Why is recursive setTimeout better?
by codewithcheese 5y ago
Why 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.