What is a self-correcting timer in JavaScript?
A timer that measures the drift (how late each tick is) and adjusts the next setTimeout delay to compensate. This keeps the interval close to the target despite setTimeout's minimum-delay behavior and event loop delays.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Build a Self-Correcting Timer in JavaScript
Because the callback itself takes time to run, the event loop may be busy, background tab throttling applies, and timer clamping adds minimum delays. These accumulate, causing drift over time.
It tracks when the next tick should fire (expected). Each tick measures the drift (Date.now() - expected). The next setTimeout delay is adjusted by the drift: interval - drift. If the drift is too large, it skips a tick to catch up.
For clocks, timers, and polling that need accuracy over time. When drift would cause visible problems (a clock showing the wrong time, polling missing events). Not for visual animations (use requestAnimationFrame).
Still have questions?
Browse all our FAQs or reach out to our support team
