window.requestIdleCallback()

Experimental: 這是一個實驗中的功能
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

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.

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() (en-US) 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 a IdleDeadline (en-US) object describing the amount of time available and whether or not the callback has been run because the timeout period expired.

options 選擇性

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 timeout will be called during the next idle period, even if doing so risks causing a negative performance impact.

Example

Specifications

Specification
requestIdleCallback() Cooperative Scheduling of Background Tasks
# the-requestidlecallback-method

Browser compatibility

BCD tables only load in the browser

See also