Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
Trying to perform a slightly different search (Timer, event listener, event dispatch?)

How I need to:

  1. First keyup on (search input) fires up search countdown timer and set timer to 1s.
  2. Every next search keyup on (search input) reset search countdown timer to 1s.
  3. When (search input) no longer updated and stay the same -
  4. Search countdown timer reached 0s and performing a search.

Why I need to (this way):

I have large database of records and using ajax method to fetch through. Every time keyup on (search input) send a request... (it's resource waste and uncomfortable to watch how page refresh every keyup). So my way is to create "typing phase" - what I mean describe above.

Only pure JS, no libraries! Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

what you need is called debounce function.

if you don't use libraries like lodash - you can take this realisation:

function debounce(f, ms) {

  let isCooldown = false;

  return function() {
    if (isCooldown) return;

    f.apply(this, arguments);

    isCooldown = true;

    setTimeout(() => isCooldown = false, ms);
  };

}

then you can code like this:

const handleSearch = (search) => {
    // your logic with fetch or whatever
}

const handleSearchDebounced = debounce(handleSearch, 300)
about 4 years ago · Juan Pablo Isaza Relatório

0

var timer=null;
document.getElementById("myInput").onkeyup = ()=>{
  clearInterval(timer);
  timer = setInterval(()=>{
            console.log("call ajax search function here."); 
            clearInterval(timer);
          },1000);
};
<input type="text" id="myInput">

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda