Skip to content

Instantly share code, notes, and snippets.

@afroald
Created September 29, 2015 14:53
Show Gist options
  • Save afroald/0a6b354711d79ca9d91a to your computer and use it in GitHub Desktop.
Save afroald/0a6b354711d79ca9d91a to your computer and use it in GitHub Desktop.
A throttle function using requestAnimationFrame instead of timers
var requestAnimationFrame = window.requestAnimationFrame;
function throttle(callback) {
var tickRequested = false;
function tick() {
callback();
tickRequested = false;
}
return function() {
if (tickRequested) {
console.warn('Dropping frame');
return;
}
tickRequested = true;
requestAnimationFrame(tick);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment