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

107
Visualizações
How to avoid multiple AJAX calls after setTimeout

I have a text field in which I respond to typing by checking the database for the number of matches, but I don't want to pummel the database with too many queries for no reason if the user types several characters quickly.

Searching the web, the advice seems to be to wrap it all in setTimeout(), but I apparently don't understand how to use it properly. Here is my code at the moment:

$(".qs-text").keyup(function() {
  if ($(this).val().length > 2) {
    setTimeout(function() {
      $.get("ajax_request.php?req=Quicksearch&qs=" + $(".qs-text").val(), function(data) {
        $('.qs-hits').text(data);
      });
    }, 500);
  } else {
    $('.qs-hits').text('-');
  }
});

It does wait 500ms, and at the end of the timeout period, it does use the final state of the field in the request - good.

However then it sends multiple identical requests (one for every character I typed) instead of just one. That defeats the purpose of having the timeout in the first place. I can almost see why the code would do that, since the keyup event fires every time, but I have no idea how to solve it. There are other questions whose titles sound like what I'm asking, but every one I've read is different enough that I can't quite apply any of them to my case.

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

0

You need cancel timeout when create a new.

var timeout = null;
$(".qs-text").keyup(function() {
  if(timeout != null) clearTimeout(timeout);
  if ($(this).val().length > 2) {
    timeout = setTimeout(function() { $.get("ajax_request.php?req=Quicksearch&qs="+$(".qs-text").val(), function(data) {
      $('.qs-hits').text(data);
    }); },500);
  } else {
    $('.qs-hits').text('-');
  }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

We usually store the timeout in variable and then clear it conditionally when call a new one. However for large web application, I suggest using web sockets for such subsequent calls for real-time experience

var timer;
$(".qs-text").keyup(function() {
    if ($(this).val().length > 2) {
        clearTimeout(timer);
        timer = setTimeout(function() {
        $.get("ajax_request.php?req=Quicksearch&qs=" + $(".qs-text").val(), function(data) {
            $('.qs-hits').text(data);
        });
        }, 500);
    } else {
        $('.qs-hits').text('-');
    }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I would recommend using something like lodash for debouncing, so you can do something like this

$(".qs-text").keyup(function() {  

   _.debounce(()=> {
     $.get("ajax_request.php?req=Quicksearch&qs="+$(".qs-text").val(), function(data) {
      $('.qs-hits').text(data);
    })
   }, 500)
  

});

for more info, https://lodash.com/docs/4.17.15#debounce

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