Use a self-correcting timer. Measure the drift with Date.now() or performance.now(), and adjust the next delay to compensate. This keeps the interval close to the target despite setTimeout's minimum-delay behavior.
Trust Issues With setTimeout in JavaScript
How do you create a precise interval despite setTimeout's inaccuracy?
The answer
More FAQs in Trust Issues With setTimeout in JavaScript
No. setTimeout guarantees a minimum delay, not an exact delay. The actual delay depends on the call stack, microtask queue, background tab throttling, and nested timer clamping. The callback runs at least after the delay, but often later.
Because the callback goes to the macrotask queue. The event loop runs it only when the call stack is empty and all microtasks are done. If synchronous code or microtasks are running, the timer waits.
Yes. To save battery, browsers throttle timers in background tabs. A setTimeout(cb, 100) in a background tab might run after 1000ms or later. Do not rely on timers for background tabs.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.