Skip to content

Function: requestAnimationFrame()

ts
function requestAnimationFrame(fn): number;

The requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes as an argument a callback to be invoked before the repaint. The callback accepts a parameter, a timestamp, which indicates the current time when callbacks queued by requestAnimationFrame() begin to fire.

This method searches for the appropriate version of requestAnimationFrame() to use, with fallbacks for older versions in use by some browsers.

Parameters

ParameterTypeDescription
fnFrameRequestCallbackA function specifying the animation to perform for each frame.

Returns

number

A numeric ID which can be passed to cancelAnimationFrame() to cancel the requested animation.

Example

ts
const el = document.createElement('div')
document.body.appendChild(el)
const id = requestAnimationFrame(() => { el.style.top = '2px' })
console.log(typeof id)

Since

1.0.0

Released under the MIT License.