I have this debounce function:
const selectElement = document.querySelector('input');
const debounce = (cb, time = 1000) => {
let timer;
return (...args) => {
console.log('run inner function')
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => cb(...args), time)
}
}
const onChange = debounce((e) => {
console.log('event', e.target.value)
})
selectElement.addEventListener('input', onChange);
<input input={onChange}/>
The code works ok, but I want to understand, how the returned function is triggered inside debounce
function, because I know that if a function returns another function I need to call it like this: debounce()()
to trigger the second one, but in our case we trigger the function only once debounce()
in addEventListener
, but how the second call happens?