Last active
April 28, 2023 22:01
-
-
Save epreston/9bb41fbbf8b34c3b11c1b07f399480bf to your computer and use it in GitHub Desktop.
wait for idle promise - runs async right before paint - safari fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const idleOptions = { timeout: 500 }; | |
const request = window.requestIdleCallback || window.requestAnimationFrame; | |
const cancel = window.cancelIdleCallback || window.cancelAnimationFrame; | |
const resolveWhenIdle = { | |
request, | |
cancel, | |
promise: (num) => new Promise((resolve) => request(resolve, Object.assign({}, idleOptions, num))), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's different from setTimeout (runs async after paint) or microtask (sync code that's pushed to the end of the current work).
It uses one of the following functions because requestIdleCallback is not supported in Safari.
This provides a consistent interface between the two.
It is used to introduce delays to allow animations or DOM updates to render as states change.
Examples: