Do something after the user stops typing

Wait a short moment after each key press, so a search runs once and not on every letter.

JavaScript · Do something after the user stops typingBeginner
const input = document.querySelector('#q');
let timer;

input.addEventListener('input', () => {
  clearTimeout(timer);
  timer = setTimeout(() => {
    console.log('search for', input.value.trim());
  }, 300);
});
inputeventsetTimeoutsearch