Which fix removes the bug?
function debounce(fn, ms) {
let t;
return (...a) => {
t = setTimeout(fn(...a), ms);
};
}
- Clear then defer:
clearTimeout(t)and wrap - Clear then defer:
clearInterval(t)and wrap - Return the timer id from the inner arrow
- Move
let tinside the returned function
fn(...a) runs immediately and nothing cancels the previous timer, so it never debounces.
Play Problem Solving ▸