Merge pull request #300 from nolanlawson/nolan/simplify-rIC

fix: improve requestIdleCallback usage
nolan/hinaloe-test
Nolan Lawson 2019-03-14 08:33:17 -07:00 committed by GitHub
commit 5dac39e179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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()
})
}
}