Created
March 1, 2019 16:53
-
-
Save iansvo/4dedfb28f97595faed110fd05c6632e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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