Skip to content

Instantly share code, notes, and snippets.

@iansvo
Created March 1, 2019 16:53
Show Gist options
  • Save iansvo/4dedfb28f97595faed110fd05c6632e1 to your computer and use it in GitHub Desktop.
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