mirror of
https://github.com/khairul169/garage-webui.git
synced 2025-12-18 13:31:04 +07:00
21 lines
460 B
TypeScript
21 lines
460 B
TypeScript
|
|
import { useCallback, useRef } from "react";
|
||
|
|
|
||
|
|
export const useDebounce = <T extends (...args: any[]) => void>(
|
||
|
|
fn: T,
|
||
|
|
delay: number = 500
|
||
|
|
) => {
|
||
|
|
const timerRef = useRef<NodeJS.Timeout | null>(null);
|
||
|
|
|
||
|
|
const debouncedFn = useCallback(
|
||
|
|
(...args: any[]) => {
|
||
|
|
if (timerRef.current) {
|
||
|
|
clearTimeout(timerRef.current);
|
||
|
|
}
|
||
|
|
timerRef.current = setTimeout(() => fn(...args), delay);
|
||
|
|
},
|
||
|
|
[fn]
|
||
|
|
);
|
||
|
|
|
||
|
|
return debouncedFn as T;
|
||
|
|
};
|