Window: requestIdleCallback() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Thewindow.requestIdleCallback()
method queues a functionto be called during a browser's idle periods. This enables developers to performbackground and low priority work on the main thread, without impactinglatency-critical events such as animation and input response. Functions are generallycalled in first-in-first-out order; however, callbacks which have atimeout
specified may be called out-of-order if necessary in order to run them before thetimeout elapses.
You can callrequestIdleCallback()
within an idle callback function toschedule another callback to take place no sooner than the next pass through the eventloop.
Note:Atimeout
option is strongly recommended for required work,as otherwise it's possible multiple seconds will elapse before the callback is fired.
Syntax
requestIdleCallback(callback)requestIdleCallback(callback, options)
Parameters
callback
A reference to a function that should be called in the near future, when the eventloop is idle. The callback function is passed an
IdleDeadline
objectdescribing the amount of time available and whether or not the callback has been runbecause the timeout period expired.options
OptionalContains optional configuration parameters. Currently only one property is defined:
timeout
If the number of milliseconds represented by this parameter has elapsed and the callback has not already been called, then a task to execute the callback is queued in the event loop (even if doing so risks causing a negative performance impact).
timeout
must be a positive value or it is ignored.
Return value
An ID which can be used to cancel the callback by passing it into thewindow.cancelIdleCallback()
method.
Examples
See ourcomplete examplein the articleCooperative Scheduling of Background Tasks API.
Specifications
Specification |
---|
requestIdleCallback() # the-requestidlecallback-method |