You’re not storing the new timer handle, so clearTimeout(id) keeps clearing undefined and every call schedules another run; in UI code that turns “debounce” into a delayed flood.
function debounce(fn, delay) {
let id;
return (...args) => {
clearTimeout(id);
id = setTimeout(() => fn(...args), delay);
};
}
Also worth noting, if this is wired to an input event, you may want to pass the value not the event object in some frameworks because pooled events can bite you.