Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active January 23, 2021 12:23
Show Gist options
  • Save ashwinkumar2438/71f166cea147c42b638bf41735af9648 to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/71f166cea147c42b638bf41735af9648 to your computer and use it in GitHub Desktop.
//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