webcoreui
Version:
UI component and template library for Astro, Svelte, and React apps styled with Sass.
25 lines (19 loc) • 501 B
text/typescript
export const debounce = (fn: any, waitFor = 100) => {
let timeout: ReturnType<typeof setTimeout> | null
const clear = () => {
if (timeout !== null) {
clearTimeout(timeout)
timeout = null
}
}
const debouncedFn = (...args: any[]) => {
clear()
timeout = setTimeout(() => {
fn(...args)
}, waitFor)
}
debouncedFn.cancel = () => {
clear()
}
return debouncedFn
}