Created
September 29, 2015 14:53
-
-
Save afroald/0a6b354711d79ca9d91a to your computer and use it in GitHub Desktop.
A throttle function using requestAnimationFrame instead of timers
This file contains 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
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