AI wrote a debounce — I made it cancelable
Started from an AI snippet for debounce. It leaked timers on unmount. Here's the fix: return a cancel function and clear on dispose.
javascript
export function debounce(fn, ms) {
let t;
const wrapped = (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), ms);
};
wrapped.cancel = () => clearTimeout(t);
return wrapped;
}Live demo
Comments (0)
Sign in to comment.