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

204
Visualizações
How can I prevent firing setTimeout function if user performs actions too fast in js?

I have this part of code:

$('#clientsfilter').on('input', function() {
     var string = $('#clientsfilter').val();
     if (string != "") {
         //console.log("input is not empty");
         var timeoutid = setTimeout(function() {
             // some stuff
             console.log("fired!");   
         }, 1500);
         console.log("timeoutid is "+timeoutid);
     }
     else
     {
        // console.log("input is empty");
     }    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="clientsfilter" type="text" size="27">

When a user types something, it perform some actions after 1,5 seconds. But if a user will type one symbol after another too fass (time between symbols less than 1,5 seconds), the setTimeout() stuff will fire every time user adds symbol (or deletes). I want to prevent firing setTimeout function too many times, only if 1.5 seconds passed from last typing. I found information about

clearTimeout(timeoutid);

But how can I use it my situation?

about 4 years ago · Juan Pablo Isaza
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