import { useEffect, useRef, useCallback } from 'react';
function useDebounceFn(fn, delay = 1000) {
const debounce = useRef<any>({ fn, timer: null });
useEffect(() => {
debounce.current.fn = fn;
}, [fn]);
return useCallback(
(...args) => {
clearTimeout(debounce.current.timer);
delete debounce.current.timer;
debounce.current.timer = setTimeout(() => {
debounce.current.fn(...args);
}, delay);
},
[delay]
);
}
export default useDebounceFn;
网友评论