mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-03-28 11:39:26 +01:00
24 lines
511 B
TypeScript
24 lines
511 B
TypeScript
import { useRef, useEffect } from "react";
|
|
|
|
const useDebounce = () => {
|
|
const timeout = useRef<number | undefined>(400);
|
|
|
|
const debounce =
|
|
(func: Function, wait: number = 0) =>
|
|
(...args: any[]) => {
|
|
clearTimeout(timeout.current);
|
|
timeout.current = window.setTimeout(() => func(...args), wait);
|
|
};
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (!timeout.current) return;
|
|
clearTimeout(timeout.current);
|
|
};
|
|
}, []);
|
|
|
|
return { debounce };
|
|
};
|
|
|
|
export default useDebounce;
|