Skip to content

Instantly share code, notes, and snippets.

@manuelricci
Created January 28, 2021 16:14
Show Gist options
  • Save manuelricci/46284b8627b9110ebdc27242c5b37799 to your computer and use it in GitHub Desktop.
Save manuelricci/46284b8627b9110ebdc27242c5b37799 to your computer and use it in GitHub Desktop.
Debounce a function in JavaScript
const debounce = (funcToExecute, executeAfter) => {
let timer;
return function(){
clearTimeout(timer);
const context = this;
const args = arguments;
timer = setTimeout(()=>{
funcToExecute.apply(context, args)}
, executeAfter);
}
}
debounce(() => this.makeAPICall(), 300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment