Skip to content

Instantly share code, notes, and snippets.

@asvny
Created July 21, 2017 11:23
Show Gist options
  • Save asvny/ecea690b5b634a4a7ef27d5ea78938e1 to your computer and use it in GitHub Desktop.
Save asvny/ecea690b5b634a4a7ef27d5ea78938e1 to your computer and use it in GitHub Desktop.
raf scheduler
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