32 lines
798 B
TypeScript
32 lines
798 B
TypeScript
import { useCallback, useRef } from "react";
|
|
|
|
/**
|
|
* 通用防抖 Hook
|
|
* @param callback 需要防抖的函数
|
|
* @param delay 防抖延迟时间(毫秒)
|
|
* @returns 包装后的防抖函数
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function useDebounce<T extends (...args: any[]) => any>(
|
|
callback: T,
|
|
delay: number,
|
|
): (...args: Parameters<T>) => void {
|
|
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
const debouncedFunction = useCallback(
|
|
(...args: Parameters<T>) => {
|
|
if (timerRef.current) {
|
|
clearTimeout(timerRef.current);
|
|
}
|
|
timerRef.current = setTimeout(() => {
|
|
callback(...args);
|
|
}, delay);
|
|
},
|
|
[callback, delay],
|
|
);
|
|
|
|
return debouncedFunction;
|
|
}
|
|
|
|
export default useDebounce;
|