Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 2x 1x | export function debounce<T extends (...args: any[]) => void>(
fn: T, delay = 300
): (...args: Parameters<T>) => void {
let timer: ReturnType<typeof setTimeout>;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
export function throttle<T extends (...args: any[]) => void>(
fn: T, limit = 300
): (...args: Parameters<T>) => void {
let last = -Infinity; // 确保首次调用立即触发(leading)
return (...args) => {
const now = Date.now();
if (now - last >= limit) {
last = now;
fn(...args);
}
};
}
export function once<T extends (...args: any[]) => any>(fn: T): T {
let called = false;
let result: ReturnType<T>;
return ((...args: Parameters<T>) => {
if (!called) {
called = true;
result = fn(...args);
}
return result;
}) as T;
}
|