fix: improve requestIdleCallback usage

release
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(). // 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()
})
} }
} }