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().
|
// Use requestIdleCallback() if available, else fall back to setTimeout().
|
||||||
// Throttle so as not to run too frequently.
|
// Throttle so as not to run too frequently.
|
||||||
function throttleIdleTask(func) {
|
function throttleIdleTask(func) {
|
||||||
const queue =
|
const doIdleTask =
|
||||||
typeof requestIdleCallback === 'function' ? requestIdleCallback : setTimeout
|
typeof requestIdleCallback === 'function' ? requestIdleCallback : setTimeout
|
||||||
const clear =
|
|
||||||
typeof cancelIdleCallback === 'function' ? cancelIdleCallback : clearTimeout
|
|
||||||
|
|
||||||
let id
|
let running = false
|
||||||
|
|
||||||
return function throttled() {
|
return function throttled() {
|
||||||
if (id) {
|
if (running) {
|
||||||
clear(id)
|
return
|
||||||
}
|
}
|
||||||
id = queue(func)
|
running = true
|
||||||
|
doIdleTask(() => {
|
||||||
|
running = false
|
||||||
|
func()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue