Last active
October 19, 2018 11:01
-
-
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
This file contains hidden or 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
| 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