requestAnimationFrame()
Schedules a callback to run before the next browser repaint.
Syntax
requestAnimationFrame(callback)
Returns
number — A request id that can be passed to `cancelAnimationFrame()`.
Examples
function step(timestamp) {
console.log("frame at", Math.round(timestamp));
}
requestAnimationFrame(step);
Output
frame at 16
let frames = 0;
function loop() {
if (++frames < 3) requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
Notes
- The callback receives a high-resolution timestamp (`DOMHighResTimeStamp`).
- Typically fires about 60 times per second, paused in background tabs.
- Re-request inside the callback to create an animation loop.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 24 |
| firefox | 23 |
| safari | 7 |
| edge | 12 |
| node | 0.10 |