Which fix removes the bug?
function debounce(fn, ms) {
let t;
return function (...args) {
clearTimeout(t);
t = setTimeout(fn(...args), ms);
};
}
- Remove
clearTimeout(t)from the body - Change to
setTimeout(() => fn(...args), ms) - Change
let ttoconst tin the closure - Move
clearTimeoutafter setTimeout
fn(...args) runs immediately; pass a function instead.