Favorites

DOM Window.requestAnimationFrame() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Window.requestAnimationFrame()
DOM Window MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The requestAnimationFrame() method runs a function just before the browser's next repaint, which makes it the right tool for smooth JavaScript animations.

Description

The requestAnimationFrame() method asks the browser to call a function before the next repaint, usually 60 or more times a second, in step with the display. To animate, update positions in the callback and request the next frame from inside it. The callback receives a timestamp, so movement can be based on elapsed time rather than frame count.

Callbacks pause in background tabs, saving battery. For simple effects, CSS transitions and animations are easier and often smoother; use requestAnimationFrame() when JavaScript has to drive each frame.

To stop an animation, keep the ID returned by the last call and pass it to cancelAnimationFrame(). Displays with high refresh rates call the callback more than 60 times a second, which is why using the timestamp, rather than assuming a fixed frame length, keeps movement at the same speed everywhere.

Syntax

requestAnimationFrame(callback)

Parameters

  • callbackThe function to run before the next repaint. It receives a high-resolution timestamp.

Return Value

A request ID, which can be passed to cancelAnimationFrame().

Example

<div id="ball" style="width:30px;height:30px;border-radius:50%;background:#2563eb"></div>
const ball = document.getElementById('ball');
let start;
function step(time) {
  start ??= time;
  const elapsed = time - start;
  ball.style.transform = `translateX(${Math.min(elapsed / 5, 200)}px)`;
  if (elapsed < 1000) requestAnimationFrame(step);
  else console.log('Animation finished');
}
requestAnimationFrame(step);

Browser Support

The following information will show you the current browser support for the Window.requestAnimationFrame() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee