-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplementThrottleWithLeading&TrailingOption.js
More file actions
24 lines (22 loc) · 1.19 KB
/
ImplementThrottleWithLeading&TrailingOption.js
File metadata and controls
24 lines (22 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function throttle(func, wait, option = {leading: true, trailing: true}) {
let hasThrottleTimedOut = null; //assigning starting values
let storedEvent = null;
return throttledEventHandle= (event, trailingTrueCase)=>{ //this is returned when someone wants to makes a function throttled, this func remembers the passed func & wait time due to clousures property.
storedEvent = event; //stores the most recently arrived event
if(!hasThrottleTimedOut ){
if( option.leading==false && !trailingTrueCase){
storedEvent = null; //nulled as event is used now
hasThrottleTimedOut =true; //throttle has timed out thus the callback func was executed.
}
else {
func(event);
storedEvent = null; //nulled as event is used now
hasThrottleTimedOut =true; //throttle has timed out thus the callback func was executed.
}
setTimeout(()=>{
hasThrottleTimedOut=null; //timout over, and we are open to execute if there are new events
if(storedEvent && option.trailing==true)throttledEventHandle(storedEvent,true); //if wait time is done & do we have a event stored within that time?, if yes, then execute it
},wait)
}
}
}