-
Notifications
You must be signed in to change notification settings - Fork 23.3k
Expand file tree
/
Copy pathindex.md
More file actions
70 lines (52 loc) · 2.49 KB
/
Copy pathindex.md
File metadata and controls
70 lines (52 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "Window: requestIdleCallback() method"
short-title: requestIdleCallback()
slug: Web/API/Window/requestIdleCallback
page-type: web-api-instance-method
browser-compat: api.Window.requestIdleCallback
---
{{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 `timeout` option is strongly recommended for required work,
> as otherwise it's possible multiple seconds will elapse before the callback is fired.
## Syntax
```js-nolint
requestIdleCallback(callback)
requestIdleCallback(callback, options)
```
### 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 {{domxref("IdleDeadline")}} object
describing the amount of time available and whether or not the callback has been run
because the timeout period expired.
- `options` {{optional_inline}}
- : Contains 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 the
{{domxref("window.cancelIdleCallback()")}} method.
## Examples
See our [complete example](/en-US/docs/Web/API/Background_Tasks_API#example)
in the article [Cooperative Scheduling of Background Tasks API](/en-US/docs/Web/API/Background_Tasks_API).
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("Window.cancelIdleCallback()")}}
- {{domxref("IdleDeadline")}}
- {{domxref("Window.setTimeout()")}}
- {{domxref("Window.setInterval()")}}
- {{domxref("Window.requestAnimationFrame()")}}