// react hook 防抖函数
function useDebounce(value, delay) { const [debouncedValue, setDebouncedValue] = useState(value); useEffect( () => { const handler = setTimeout(() => { setDebouncedValue(value); }, delay); return () => { clearTimeout(handler); }; }, [value, delay] ); return debouncedValue; }
// react class 防抖 function debounce(fn, wait) { let timeout = null; return () => { if (timeout !== null) clearTimeout(timeout); timeout = setTimeout(fn, wait); }; }