Creating fluid visual animations in web applications requires synchronizing JavaScript execution with the display hardware's vertical synchronization (V-Sync). Using setInterval or setTimeout for animations causes frame drops, stuttering, and wasted CPU cycles. requestAnimationFrame() is the browser's native API for high-performance visual animations.
setTimeout & setInterval Fail at AnimationsMost computer and mobile displays refresh at 60Hz (once every 16.67ms) or 120Hz (once every 8.33ms).
setInterval keeps running even when the user switches tabs, draining laptop battery and mobile CPU!setTimeout to 1,000ms in inactive tabs.requestAnimationFrame Mechanicswindow.requestAnimationFrame(callback):
performance.now()) to the callback.cancelAnimationFrame()To stop an in-flight animation loop (e.g. on user click, modal close, or component unmount):
To ensure game physics and animations run at the exact same speed on 60Hz, 120Hz, and 144Hz monitors, always calculate Delta Time ($\Delta t$):
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Critical Rendering Path & Reflow/Repaint | Memory Management & Garbage Collection |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.