18 lines
394 B
TypeScript
18 lines
394 B
TypeScript
|
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)
|
||
|
}
|
||
|
}
|