-
Notifications
You must be signed in to change notification settings - Fork 2
/
throttle.ts
36 lines (29 loc) · 1.04 KB
/
throttle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { TAnyFunction } from '../typeHelpers'
/**
* ### throttle(func, wait)
*
* Create a throttled function that invokes `func` at most every `wait` milliseconds.
* If the invocation is throttled, `func` will be invoked with the last arguments provided.
*
* ```js
* const func = () => console.log('Heavy processing happening')
* const throttledFunc = flocky.throttle(func, 250)
* ```
*/
export function throttle<TFunc extends TAnyFunction<void>>(func: TFunc, wait: number) {
let timeoutID: NodeJS.Timeout | null = null
let lastCall = 0
return function (this: unknown, ...args: unknown[]) {
timeoutID && clearTimeout(timeoutID)
const remainingWait = wait - (Date.now() - lastCall)
const callFunc = () => {
func.apply(this, args)
lastCall = Date.now()
}
// Immediately call the function if we don't have to throttle it
if (remainingWait <= 0 || remainingWait > wait) {
return callFunc()
}
timeoutID = setTimeout(() => callFunc(), remainingWait)
} as (...args: Parameters<TFunc>) => void
}