requestAnimationFrame()
A function that schedules a callback to run just before the browser's next repaint, the right place to update animations.
In more detail
The callback receives a high-resolution timestamp for smooth timing. Browsers pause these callbacks in hidden tabs, saving power. Unlike setTimeout it stays in step with the display.
Related terms
- VSync — The signal a display sends when it is ready to show a new frame, which the browser uses to time its rendering.
- frame budget — The time available to produce one frame, about 16.7 milliseconds at 60 frames per second, shared by your code and the browser's own work.
- requestIdleCallback — A function that schedules low-priority work to run when the browser has spare time at the end of a frame.
- DOMHighResTimeStamp — A time value in milliseconds with fractions, measured from when the page started loading, used by performance.now() and animation callbacks.
Taught in
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- requestAnimationFrame vs requestIdleCallback: High-FPS Work — Frontend Architecture & Web Platform