Facebook Pixel

Why does setInterval drift over time 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.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How to Build 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.

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

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.