Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

debounce events in JS

const debounce = (callback, time) => {
  let debounceTimer;
  return (e) => {
    window.clearTimeout(debounceTimer);
    debounceTimer = window.setTimeout(callback.bind(null, e), time);
  };
};

function handleInput(e) {
  console.log(e.target.value);
}

document
  .querySelector('#input')
  .addEventListener('input', debounce(handleInput, 1000));
 
PREVIOUS NEXT
Tagged: #debounce #events #JS
ADD COMMENT
Topic
Name
5+3 =