Skip to content

Instantly share code, notes, and snippets.

@betojsx
Created September 12, 2018 14:13
Show Gist options
  • Save betojsx/81d9e1770a6202f2f4bc61af1a7281e9 to your computer and use it in GitHub Desktop.
Save betojsx/81d9e1770a6202f2f4bc61af1a7281e9 to your computer and use it in GitHub Desktop.
Debounce Function in good old Vanilla JavaScript
function debounce(func, wait, 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