Skip to content

Instantly share code, notes, and snippets.

@shafiimam
Created June 21, 2022 11:04
Show Gist options
  • Save shafiimam/86995bb1b24ac5d29956431383b02927 to your computer and use it in GitHub Desktop.
Save shafiimam/86995bb1b24ac5d29956431383b02927 to your computer and use it in GitHub Desktop.
JS debounce
function debounce(func, wait=700, immediate) {
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