requestAnimationFrame()

Schedules a callback to run before the next browser repaint.

Since ES5 Spec ↗

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

EnvironmentSince version
chrome 24
firefox 23
safari 7
edge 12
node 0.10

See also