mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-28 16:49:36 +07:00
27 lines
595 B
TypeScript
27 lines
595 B
TypeScript
import { useCallback, useRef } from "react";
|
|
|
|
export const useDebounce = (fn: Function, delay = 500) => {
|
|
const timerRef = useRef<any>(null);
|
|
|
|
const cancel = useCallback(() => {
|
|
if (timerRef.current) {
|
|
clearTimeout(timerRef.current);
|
|
timerRef.current = null;
|
|
}
|
|
}, []);
|
|
|
|
const debounce = useCallback(
|
|
(...params: any[]) => {
|
|
cancel();
|
|
|
|
timerRef.current = setTimeout(() => {
|
|
fn(...params);
|
|
timerRef.current = null;
|
|
}, delay);
|
|
},
|
|
[fn, delay, cancel]
|
|
);
|
|
|
|
return [debounce, cancel] as [typeof debounce, typeof cancel];
|
|
};
|