Skip to content

Instantly share code, notes, and snippets.

@metasansana
Created September 2, 2017 22:32
Show Gist options
  • Save metasansana/beb3e06d423cfbf518eb565e4829ac28 to your computer and use it in GitHub Desktop.
Save metasansana/beb3e06d423cfbf518eb565e4829ac28 to your computer and use it in GitHub Desktop.
Quick JavaScript/Typescript debounce function.
const debounce = (f, delay) => {
let timer = null;
return a=> {
if(!timer) {
timer = setTimeout(a=>f(a), delay);
}else{
clearTimeout(timer);
timer = setTimeout(a=>f(a), delay);
}
}
}
const debounce = <A>(f:(a:A)=>void, delay:number) => {
let timer:number = null;
return (a:A)=> {
if(!timer) {
timer = setTimeout(()=>f(a), delay);
}else{
clearTimeout(timer);
timer = setTimeout(()=>f(a), delay);
}
}
}
@quantuminformation
Copy link

You can't do let timer:number = null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment