setTimeout Minimum Delay and Clamping in JavaScript
Browsers clamp setTimeout delays. Here is what the minimum delays are and why.
setTimeout Minimum Delay and Clamping in JavaScript
Browsers enforce minimum delays on setTimeout in certain situations. Here is what they are and why.
Nested Timer Clamping (4ms)
After 5 nested setTimeout calls, browsers may enforce a 4ms minimum. This is specified in the HTML spec to prevent abuse.
Background Tab Throttling (1000ms)
In background tabs, browsers throttle timers to save battery. Chrome and Firefox enforce a minimum of about 1 second for setTimeout in background tabs.
Why Clamping Exists
- Performance: too many zero-delay timers would flood the event loop.
- Battery: background tabs do not need precise timers.
- Security: prevent timing attacks and excessive resource use.
What to Do
- Do not rely on 0ms timers (clamped to 4ms after 5 levels).
- Do not rely on timers in background tabs (throttled to 1000ms+).
- Use
requestAnimationFramefor animations. - Use
performance.now()to measure elapsed time. - Use a self-correcting timer for precise intervals.
The Takeaway
Browsers clamp setTimeout delays: 4ms minimum after 5 nested levels, and ~1000ms in background tabs. This is for performance, battery, and security. Use requestAnimationFrame for animations and performance.now() for timing.
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).
Measure the actual delay with Date.now() or performance.now(). Compare the expected delay to the actual delay. If the actual delay is much larger, the timer is being throttled (likely in a background tab).
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.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

