requestAnimationFrame vs Timers for Visual JavaScript Work
Schedule visual updates around browser paints and reserve timers for elapsed-time tasks.
requestAnimationFrame schedules a callback before a future repaint, making it a natural place to coordinate visual updates. Timers schedule after a delay and are useful for elapsed-time behavior, but do not align work to the display refresh cycle.
THE PRODUCTION PROBLEM STATEMENT
A loop built with setTimeout can drift, run at uneven rates, or perform unnecessary visual work between frames. A callback scheduled too often may continue after its UI has disappeared.
Let the browser choose the next paint opportunity
The animation-frame callback receives a high-resolution timestamp. Advance an animation using the difference between timestamps instead of assuming every display refresh takes the same number of milliseconds.
Browsers commonly pause animation callbacks in background tabs. Clamp large elapsed-time jumps when resuming would otherwise make an animation appear broken.
Separate layout reads from visual writes
Repeatedly reading geometry after changing styles can force synchronous layout. Collect required measurements first, then apply visual writes together. Prefer transforms and opacity when they express the effect without changing document flow.
requestAnimationFrame is a scheduling point, not a guarantee that expensive code becomes cheap. Keep callbacks small, profile the page, and move suitable heavy computation elsewhere.
Use timers for delays, not frame pacing
setTimeout is appropriate for a debounce delay, retry backoff, or reminder after inactivity. Its requested delay is a minimum; throttling and main-thread load can make the callback run later.
Avoid a permanent timer loop to repaint visual state. Stop scheduling when work is no longer useful, cancel a pending frame during cleanup, and resume from current state when needed.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. It follows the display and browser scheduling conditions, which vary by device, refresh rate, visibility, and workload.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
The JavaScript Event Loop in Depth: Microtasks, Macrotasks, and Browser Frame Budgeting
An exhaustive dissection of V8 execution contexts, microtask queue drainage, task scheduling, requestAnimationFrame, and preventing main-thread UI jank.
Migrating from Legacy Date to the JavaScript Temporal API: Immutability and Time Zone Precision
A comprehensive migration handbook for replacing the 30-year-old Date object with immutable, IANA-aware Temporal primitives.