💻 CODE

Fonction debounce en JavaScript

📄 debounce.js JAVASCRIPT
/**
 * Retarde l'exécution de fn jusqu'à ce que
 * wait ms se soient écoulés sans nouvel appel.
 */
function debounce(fn, wait = 300) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), wait);
  };
}

// Usage
const onSearch = debounce((query) => {
  console.log("Searching:", query);
}, 400);

document.querySelector("#search").addEventListener("input", (e) => {
  onSearch(e.target.value);
});