File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 1
1
//From: https://kettanaito.com/blog/debounce-vs-throttle
2
2
3
- /** A very simple throttle. Will execute the function every Xms and discard any other calls during that period. */
3
+ /** A very simple throttle. Will execute the function at the end of each period and discard any other calls during that period. */
4
4
export function throttle (
5
5
func : ( ...args : any [ ] ) => void ,
6
- duration : number
6
+ durationMs : number
7
7
) : ( ...args : any [ ] ) => void {
8
- let shouldWait = false ;
8
+ let isPrimedToFire = false ;
9
9
10
10
return ( ...args : any [ ] ) => {
11
- if ( ! shouldWait ) {
12
- func ( ...args ) ;
13
- shouldWait = true ;
11
+ if ( ! isPrimedToFire ) {
12
+ isPrimedToFire = true ;
14
13
15
14
setTimeout ( ( ) => {
16
- shouldWait = false ;
17
- } , duration ) ;
15
+ func ( ...args ) ;
16
+ isPrimedToFire = false ;
17
+ } , durationMs ) ;
18
18
}
19
19
} ;
20
20
}
You can’t perform that action at this time.
0 commit comments