Created
July 21, 2017 11:23
-
-
Save asvny/ecea690b5b634a4a7ef27d5ea78938e1 to your computer and use it in GitHub Desktop.
raf scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rafScheduler = (fn) => { | |
let lastArgs = []; | |
let frameId = null; | |
return (...args) => { | |
// Always capture the latest value | |
lastArgs = args; | |
// There is already a frame queued | |
if (frameId) { | |
return frameId; | |
} | |
// Schedule a new frame | |
frameId = requestAnimationFrame(() => { | |
frameId = null; | |
fn(...lastArgs); | |
}); | |
return frameId; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment