Last active
December 21, 2022 01:27
-
-
Save ghostcode/65b0f7455d48e2375650cc114e13e7c4 to your computer and use it in GitHub Desktop.
throttle
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
| function throttle(fn,wait=3000){ | |
| let lastTime = Date.now(), | |
| timeFlag = null | |
| return function(...args){ | |
| let current = Date.now() | |
| clearTimeout(timeFlag) | |
| if(current - lastTime >= wait){ | |
| fn.apply(this,args) | |
| lastTime = current | |
| }else{ | |
| timeFlag = setTimeout(()=>{ | |
| fn.apply(this,args) | |
| lastTime = current | |
| },wait) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment