I don't understand why this works. wouldn't the >>> let timeoutID <<<< reset the variable each time, so that the timeoutId is always empty? I would put the 'let' outside of this assuming that's the only way it would work. However this works, I just don't get it. This is from a Udemy course. I get everything else. I'm an amateur here, so.....
const debounce = (func, delay = 1000) => {
let timeoutId;
return (...args) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func.apply(null,args);
},delay)
};
};
const onInput = event => {
fetchData(event.target.value);
};
input.addEventListener('input', debounce(onInput,1000));