Created
June 25, 2020 15:11
-
-
Save mrkitan/9d0052a1ecc68ce939825ee4f4741bde to your computer and use it in GitHub Desktop.
debounce TypeScript
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
| const debounce = (fn: (...params: any[]) => any, delay: number, immed: boolean = false) => { | |
| let timer: number | undefined = undefined; | |
| return function(this: any, ...args: any[]) { | |
| if (timer === undefined && immed) { | |
| fn.apply(this, args); | |
| } | |
| clearTimeout(timer); | |
| timer = setTimeout(() => fn.apply(this, args), delay); | |
| return timer; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment