Merge pull request #300 from nolanlawson/nolan/simplify-rIC
fix: improve requestIdleCallback usagenolan/hinaloe-test
commit
5dac39e179
|
@ -192,18 +192,20 @@ function measureScrollbar() {
|
|||
// Use requestIdleCallback() if available, else fall back to setTimeout().
|
||||
// Throttle so as not to run too frequently.
|
||||
function throttleIdleTask(func) {
|
||||
const queue =
|
||||
const doIdleTask =
|
||||
typeof requestIdleCallback === 'function' ? requestIdleCallback : setTimeout
|
||||
const clear =
|
||||
typeof cancelIdleCallback === 'function' ? cancelIdleCallback : clearTimeout
|
||||
|
||||
let id
|
||||
let running = false
|
||||
|
||||
return function throttled() {
|
||||
if (id) {
|
||||
clear(id)
|
||||
if (running) {
|
||||
return
|
||||
}
|
||||
id = queue(func)
|
||||
running = true
|
||||
doIdleTask(() => {
|
||||
running = false
|
||||
func()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue