( fn: (...args: TArgs) => void, thisArg: any, )
| 25 | * Latest arguments are used on the actual call |
| 26 | */ |
| 27 | export function throttled<TArgs extends Array<any>>( |
| 28 | fn: (...args: TArgs) => void, |
| 29 | thisArg: any, |
| 30 | ) { |
| 31 | let argsToUse = [] as TArgs; |
| 32 | let ticking = false; |
| 33 | |
| 34 | return function(...args: TArgs) { |
| 35 | // Save the args for use later |
| 36 | argsToUse = args; |
| 37 | if (!ticking) { |
| 38 | ticking = true; |
| 39 | requestAnimFrame.call(window, () => { |
| 40 | ticking = false; |
| 41 | fn.apply(thisArg, argsToUse); |
| 42 | }); |
| 43 | } |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Debounces calling `fn` for `delay` ms |
no test coverage detected