Skip to content

Instantly share code, notes, and snippets.

@gregorypratt
Last active October 19, 2018 11:01
Show Gist options
  • Select an option

  • Save gregorypratt/e80fedcf02ffa3a73a8dd3f5b4c7200b to your computer and use it in GitHub Desktop.

Select an option

Save gregorypratt/e80fedcf02ffa3a73a8dd3f5b4c7200b to your computer and use it in GitHub Desktop.
debounce function in TypeScript based on David Walsh's blog - https://davidwalsh.name/javascript-debounce-function
export default (func: Function, wait: number = 100, immediate?: boolean) => {
let timeout;
return (...args: any[]) => {
const later = () => {
timeout = null;
if (!immediate) func(...args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func(...args);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment