Skip to content

Instantly share code, notes, and snippets.

@iansvo
Created March 1, 2019 16:53
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save iansvo/4dedfb28f97595faed110fd05c6632e1 to your computer and use it in GitHub Desktop.
// Inspired by @desandro's debounce.js https://gist.github.com/desandro/8559356
// Inspired by @ChrisFerdinandi's article: https://gomakethings.com/debouncing-events-with-requestanimationframe-for-better-performance/
function debounce( fn ) {
var timeout;
return function debounced() {
var _this = this,
args = arguments;
// If there's a timer, cancel it
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Setup the new requestAnimationFrame()
timeout = window.requestAnimationFrame(function () {
fn.apply( _this, args );
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment