Middle
What is debounce?
sobes.tech AI
Answer from AI
Debounce is a programming technique that limits the frequency of function calls. It is used so that a function is executed only after a certain amount of time has passed since the last call. This is useful, for example, when handling user input events (such as text input), to avoid calling the function on every keystroke, but only after the user stops typing.
Example in JavaScript:
function debounce(func, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func.apply(this, args), delay);
};
}
const onInput = debounce(() => {
console.log('Input finished');
}, 300);
inputElement.addEventListener('input', onInput);