Skip to content

Instantly share code, notes, and snippets.

@jshcrowthe
Last active April 19, 2018 20:59
Show Gist options
  • Save jshcrowthe/a10263f6f88d274a309b156447f97488 to your computer and use it in GitHub Desktop.
Save jshcrowthe/a10263f6f88d274a309b156447f97488 to your computer and use it in GitHub Desktop.
Javascript Event Debounce (Run function X only once after Y ms)
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
@jshcrowthe
Copy link
Author

Example Usage:

document.addEventListener('mousemove', debounce(function (event) {
  console.log('tick');
}, 1000));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment