Created
April 22, 2019 18:36
-
-
Save rgabs/1a79043da6022035ee5bb17cf63129ec to your computer and use it in GitHub Desktop.
simple throttle and debounce
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, delay) => { | |
| let timer; | |
| return (...args) => { | |
| clearTimeout(timer); | |
| timer = setTimeout(() => { | |
| fn(...args); | |
| }, delay); | |
| } | |
| }; | |
| const throttle = (fn, delay) => { | |
| let timer; | |
| return (...args) => { | |
| if(!timer) { | |
| timer = setTimeout(() => { | |
| fn(...args); | |
| timer = null; | |
| }, delay); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment