DOM Window.requestAnimationFrame() Method
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
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.
Desktop





Tablets & Mobile





