What should you use instead of setTimeout for timing-sensitive code?
Use requestAnimationFrame for animations. Use performance.now() for measuring elapsed time. Use a self-correcting timer for precise intervals. Do not rely on setTimeout for exact timing.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in setTimeout Minimum Delay and Clamping in JavaScript
After 5 nested levels, browsers enforce a 4ms minimum. This is specified in the HTML spec to prevent abuse. The first 5 levels can use 0ms, but after that, the delay is clamped to 4ms.
Yes. To save battery, browsers throttle timers in background tabs. Chrome and Firefox enforce a minimum of about 1 second for setTimeout in background tabs. Very long-backgrounded tabs may be throttled even more.
For performance (prevent flooding the event loop with zero-delay timers), battery (background tabs do not need precise timers), and security (prevent timing attacks and excessive resource use).
Still have questions?
Browse all our FAQs or reach out to our support team
