mirror of
https://github.com/kou029w/quot.git
synced 2025-01-19 00:18:09 +00:00
22 lines
437 B
TypeScript
22 lines
437 B
TypeScript
|
function throttle<Fn extends (...args: any[]) => any>(
|
||
|
fn: Fn,
|
||
|
ms: number
|
||
|
): (...args: Parameters<Fn>) => void {
|
||
|
let throttledFn = () => undefined;
|
||
|
let throttled: boolean = false;
|
||
|
|
||
|
return (...args: Parameters<Fn>): void => {
|
||
|
throttledFn = () => fn(...args);
|
||
|
if (throttled) return;
|
||
|
|
||
|
setTimeout(() => {
|
||
|
throttledFn();
|
||
|
throttled = false;
|
||
|
}, ms);
|
||
|
|
||
|
throttled = true;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export default throttle;
|