pinafore/src/routes/_utils/throttleRaf.js

19 wiersze
389 B
JavaScript

// ensure callback is only executed once per raf
export const throttleRaf = () => {
let rafCallback
let rafQueued
return function throttledRaf (callback) {
rafCallback = callback
if (!rafQueued) {
rafQueued = true
requestAnimationFrame(() => {
const cb = rafCallback
rafCallback = null
rafQueued = false
cb()
})
}
}
}