fix: improve requestIdleCallback usage

nolan/hinaloe-test
Nolan Lawson 2019-03-13 08:54:40 -07:00
parent af29bd19b0
commit d1978c096f
1 changed files with 9 additions and 7 deletions

View File

@ -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()
})
}
}