Initial commit
This commit is contained in:
21
public/utils.ts
Normal file
21
public/utils.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// A simple debounce function
|
||||
export const debounce = <F extends (...args: any[]) => any>(func: F, waitFor: number) => {
|
||||
let timeout: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const debounced = (...args: Parameters<F>) => {
|
||||
if (timeout !== null) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
timeout = setTimeout(() => func(...args), waitFor);
|
||||
};
|
||||
|
||||
const cancel = () => {
|
||||
if (timeout !== null) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
};
|
||||
|
||||
return [debounced, cancel] as [(...args: Parameters<F>) => void, () => void];
|
||||
};
|
||||
Reference in New Issue
Block a user