2023-08-24 15:59:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* 防抖函数,接受一个函数和一个延时时间
|
|
|
|
|
*/
|
2023-08-24 15:32:56 +00:00
|
|
|
|
export type DebouncedFunction<T extends (...args: any[]) => any> = (
|
|
|
|
|
...args: Parameters<T>
|
|
|
|
|
) => void
|
|
|
|
|
|
|
|
|
|
export function debounce<T extends (...args: any[]) => any>(
|
|
|
|
|
func: T,
|
|
|
|
|
delay: number
|
|
|
|
|
): DebouncedFunction<T> {
|
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout>
|
|
|
|
|
|
|
|
|
|
return (...args) => {
|
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
|
func(...args)
|
|
|
|
|
}, delay)
|
|
|
|
|
}
|
|
|
|
|
}
|