Initial commit
This commit is contained in:
24
src/utils/throttle.ts
Normal file
24
src/utils/throttle.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export function throttle<T extends (...args: unknown[]) => void>(
|
||||
func: T,
|
||||
wait: number
|
||||
): (...args: Parameters<T>) => void {
|
||||
let inThrottle: boolean = false;
|
||||
let lastArgs: Parameters<T> | null = null;
|
||||
|
||||
return function (...args: Parameters<T>) {
|
||||
if (!inThrottle) {
|
||||
func(...args);
|
||||
inThrottle = true;
|
||||
|
||||
setTimeout(() => {
|
||||
inThrottle = false;
|
||||
if (lastArgs) {
|
||||
func(...lastArgs);
|
||||
lastArgs = null;
|
||||
}
|
||||
}, wait);
|
||||
} else {
|
||||
lastArgs = args;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user