Created
September 2, 2017 22:32
-
-
Save metasansana/beb3e06d423cfbf518eb565e4829ac28 to your computer and use it in GitHub Desktop.
Quick JavaScript/Typescript debounce function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can't do
let timer:number = null;