Skip to content

Instantly share code, notes, and snippets.

@tzi
Last active November 21, 2022 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzi/5b5040b719296668528f7c67f54230cc to your computer and use it in GitHub Desktop.
Save tzi/5b5040b719296668528f7c67f54230cc to your computer and use it in GitHub Desktop.
function debounce(func, wait) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};

Debounce

A debounce function in JavaScript, inspired from David Walsh's Blog post.

Usage

window.addEventListener("resize", debounce(function(event) {
  console.log(event);
}, 150));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment