UNPKG

@storm-stack/utilities

Version:

This package includes various base utility class and various functions to assist in the development process.

27 lines (26 loc) 609 B
export function debounce(func, debounceMs, { signal } = {}) { let timeoutId = null; const debounced = function(...args) { if (timeoutId !== null) { clearTimeout(timeoutId); } if (signal?.aborted) { return; } timeoutId = setTimeout(() => { func(...args); timeoutId = null; }, debounceMs); }; const onAbort = () => { debounced.cancel(); }; debounced.cancel = () => { if (timeoutId !== null) { clearTimeout(timeoutId); timeoutId = null; } }; signal?.addEventListener("abort", onAbort, { once: true }); return debounced; }