What should you use instead of setTimeout for animations?
requestAnimationFrame. It syncs with the browser's refresh rate (usually 60fps) and runs the callback before the next paint. It is smoother and more efficient than setTimeout for visual updates.
Verify This Answer
Cross-check this information using these trusted sources:
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
