Web/API/Window/requestIdleCallback

From Get docs

This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.


The window.requestIdleCallback() method queues a function to be called during a browser's idle periods. This enables developers to perform background and low priority work on the main event loop, without impacting latency-critical events such as animation and input response. Functions are generally called in first-in-first-out order; however, callbacks which have a timeout specified may be called out-of-order if necessary in order to run them before the timeout elapses.

You can call requestIdleCallback() within an idle callback function to schedule another callback to take place no sooner than the next pass through the event loop.

A timeout option is strongly recommended for required work, as otherwise it's possible multiple seconds will elapse before the callback is fired.

Syntax

var handle = window.requestIdleCallback(callback[, options])

Return value

An ID which can be used to cancel the callback by passing it into the window.cancelIdleCallback() method.

Parameters

callback
A reference to a function that should be called in the near future, when the event loop is idle. The callback function is passed an IdleDeadline object describing the amount of time available and whether or not the callback has been run because the timeout period expired.
options Optional
Contains optional configuration parameters. Currently only one property is defined:
timeout
  • If timeout is specified and has a positive value, and the callback has not already been called by the time timeout milliseconds have passed, the callback will be called during the next idle period, even if doing so risks causing a negative performance impact.

Example

See our complete example in the article Cooperative Scheduling of Background Tasks API.

Specifications

Specification Status Comment
Cooperative Scheduling of Background Tasks Proposed Recommendation Initial definition.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

requestIdleCallback

Experimental'

Chrome

Full support 47

Edge

Full support 79

Firefox Full support 55

Notes'

Full support 55

Notes'

Notes' Enabled by default. No support 53 — 55

Notes'

Notes' Implemented but disabled by default.

IE

No support No

Opera

Full support 34

Safari

No support No

WebView Android

Full support 47

Chrome Android

Full support 47

Firefox Android Full support 55

Notes'

Full support 55

Notes'

Notes' Enabled by default. No support 53 — 55

Notes'

Notes' Implemented but disabled by default.

Opera Android

Full support 34

Safari iOS

No support No

Samsung Internet Android

Full support 5.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
See implementation notes.'
See implementation notes.


See also