mirror of
https://github.com/kou029w/quot.git
synced 2025-01-19 08:28:09 +00:00
21 lines
443 B
TypeScript
21 lines
443 B
TypeScript
function throttle<Fn extends (...args: any[]) => any>(
|
|
fn: Fn,
|
|
ms: number = 333
|
|
): (...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;
|