Skip to content

Instantly share code, notes, and snippets.

@dropletmedia
Created July 22, 2021 08:22
Show Gist options
  • Select an option

  • Save dropletmedia/2d872243af83b223cdf3c107bdcc3a42 to your computer and use it in GitHub Desktop.

Select an option

Save dropletmedia/2d872243af83b223cdf3c107bdcc3a42 to your computer and use it in GitHub Desktop.
vanilla js debounce fn
function debounce(func, wait = 20, immediate = true) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment