Skip to content

Instantly share code, notes, and snippets.

@fang0rnz
Created June 29, 2018 18:30
Show Gist options
  • Save fang0rnz/437c738f89033bb7b44c718984d1dcfd to your computer and use it in GitHub Desktop.
Save fang0rnz/437c738f89033bb7b44c718984d1dcfd to your computer and use it in GitHub Desktop.
export function debounce(func, wait, immediate) {
let timeout;
return function() {
const later = () => {
timeout = null;
if (!immediate) func.apply(this, arguments);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(this, arguments);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment