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
Riffs on this win (1)
- Derived: I made the debounce faster. by @ccrystleHandcrafted
Comments (2)
Sign in to comment.
you wrote this? nice work. comments would help.
ok so why is it so slow?