files/en-us/web/api/window/requestidlecallback/index.md
{{APIRef("HTML DOM")}}
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 thread, 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.
[!NOTE] A
timeoutoption is strongly recommended for required work, as otherwise it's possible multiple seconds will elapse before the callback is fired.
requestIdleCallback(callback)
requestIdleCallback(callback, options)
callback
options {{optional_inline}}
timeout
timeout must be a positive value or it is ignored.An ID which can be used to cancel the callback by passing it into the {{domxref("window.cancelIdleCallback()")}} method.
See our complete example in the article Cooperative Scheduling of Background Tasks API.
{{Specifications}}
{{Compat}}