Last active
January 23, 2021 12:23
-
-
Save ashwinkumar2438/71f166cea147c42b638bf41735af9648 to your computer and use it in GitHub Desktop.
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
//takes the function to be throttled and time delay in between subsequent fires as parameters. | |
//returns the passed function wrapped with settimeout and pass check. Takes latest arguments in subsequent calls. | |
let throttle=(fn,timedelay)=>{ | |
let ref={pass:true,arguments:[]}; | |
return function(){ | |
ref.arguments=arguments; | |
if(ref.pass){ | |
setTimeout(()=>{ | |
fn.apply(this,ref.arguments); | |
ref.pass=true; | |
},timedelay); | |
ref.pass=false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment