UNPKG

@bigmi/client

Version:

Reactive primitives for Bitcoin apps.

26 lines (22 loc) 543 B
export function debounce<T extends (...args: any[]) => void>( fn: T, delay: number ): T & { cancel: () => void } { let timeoutId: NodeJS.Timeout | undefined const debounced = ((...args: Parameters<T>) => { if (timeoutId) { clearTimeout(timeoutId) } timeoutId = setTimeout(() => { fn(...args) timeoutId = undefined }, delay) }) as T & { cancel: () => void } debounced.cancel = () => { if (timeoutId) { clearTimeout(timeoutId) timeoutId = undefined } } return debounced }