Skip to content

Commit afdd3f3

Browse files
committed
feat: add debounce and throttle utilities
1 parent 263d191 commit afdd3f3

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/utils/debounce.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function debounce<T extends (...args: any[]) => void>(fn: T, delayMs: number): T {
2+
let timer: any;
3+
return ((...args: any[]) => {
4+
clearTimeout(timer);
5+
timer = setTimeout(() => fn(...args), delayMs);
6+
}) as T;
7+
}
8+
9+
export function throttle<T extends (...args: any[]) => void>(fn: T, intervalMs: number): T {
10+
let lastCall = 0;
11+
return ((...args: any[]) => {
12+
const now = Date.now();
13+
if (now - lastCall >= intervalMs) {
14+
lastCall = now;
15+
fn(...args);
16+
}
17+
}) as T;
18+
}

0 commit comments

Comments
 (0)