When should you use requestAnimationFrame instead of setTimeout?
For animations and visual DOM updates. requestAnimationFrame syncs with the browser's refresh rate (usually 60fps), runs before the next paint, and produces smoother animations than setTimeout.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in setTimeout vs requestAnimationFrame vs queueMicrotask in JavaScript
setTimeout is a macrotask (lowest priority, runs after rendering). requestAnimationFrame runs before the next paint (for animations). queueMicrotask is a microtask (highest priority, runs before rendering and macrotasks).
For urgent work that must run before the next event or paint. queueMicrotask adds the callback to the microtask queue, which the event loop drains before rendering and macrotasks.
Sync code runs first. Then microtasks (queueMicrotask, Promise.then). Then requestAnimationFrame (before the next paint). Then setTimeout (macrotask, after rendering).
Still have questions?
Browse all our FAQs or reach out to our support team
