There is a search script:

var suggest_count = 0; var input_initial_value = ''; var suggest_selected = 0; $(window).load(function(){ // Ρ‡ΠΈΡ‚Π°Π΅ΠΌ Π²Π²ΠΎΠ΄ с ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρ‹ $("#search_box").keyup(function(I){ // опрСдСляСм ΠΊΠ°ΠΊΠΈΠ΅ дСйствия Π½ΡƒΠΆΠ½ΠΎ Π΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρƒ switch(I.keyCode) { // ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠ΅ΠΌ наТатия Π½Π° эти ΠΊΠ»Π°Π²ΠΈΡˆΡ‹ case 13: // enter case 27: // escape case 38: // стрСлка Π²Π²Π΅Ρ€Ρ… case 40: // стрСлка Π²Π½ΠΈΠ· break; default: // ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ поиск Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΏΡ€ΠΈ Π²Π²ΠΎΠ΄Π΅ Π±ΠΎΠ»Π΅Π΅ 2Ρ… символов if($(this).val().length>1){ input_initial_value = $(this).val(); var id_cabinet = $(this).data("id-cabinet"); // ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ AJAX запрос ΠΊ /ajax/ajax.php, ΠΏΠ΅Ρ€Π΅Π΄Π°Π΅ΠΌ Π΅ΠΌΡƒ GET query, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΌΡ‹ ΠΏΠΎΠΌΠ΅Ρ‰Π°Π΅ΠΌ наш запрос jQuery.ajax({ type: "post", url: "/search.php?vid=global", data: "search="+$(this).val()+"", dataType: 'json', success: function(data){ if(data.succes == 1){ // ΠΏΠ΅Ρ€Π΅Π΄ ΠΏΠΎΠΊΠ°Π·ΠΎΠΌ слоя подсказки, Π΅Π³ΠΎ обнуляСм $("#search_advice_wrapper").html("").show(); // добавляСм слою ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ $('#search_advice_wrapper').append(''+data.search_content+''); $('.search-line').bind('click', function () { ... }); } else { // добавляСм слою ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ $("#search_advice_wrapper").html("").show(); $('#search_advice_wrapper').append(''+data.search_content+''); } } }); } break; } }); // Π΄Π΅Π»Π°Π΅ΠΌ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΡƒ ΠΊΠ»ΠΈΠΊΠ° ΠΏΠΎ подсказкС $('.advice_variant').on('click',function(){ // ставим тСкст Π² input поиска $('#search_box').val($(this).text()); // прячСм слой подсказки $('#search_advice_wrapper').fadeOut(350).html(''); }); // Ссли ΠΊΠ»ΠΈΠΊΠ°Π΅ΠΌ Π½Π° ΠΏΠΎΠ»Π΅ input ΠΈ Π΅ΡΡ‚ΡŒ ΠΏΡƒΠ½ΠΊΡ‚Ρ‹ подсказки, Ρ‚ΠΎ ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ скрытый слой $('#search_box').click(function(event){ //alert(suggest_count); if(suggest_count) $('#search_advice_wrapper').show(); event.stopPropagation(); }); }); 

If you type a slow query, for example, by id "7202" it looks good, i.e. straight slowly you type, if you quickly type "7202", it gives out data and you can see what I was looking for only on "720" ...

What could be? Maybe someone did a smart delay?

    1 answer 1

    In this code, several concurrent ajax requests will be created, the result of which will work unpredictably.

    When creating a query, you can save it to a variable:

     var request = $.ajax({ type: 'POST', url: 'url', success: function(result){} }); 

    In subsequent requests you need to break the previous one:

     request.abort(); 

    And launch a new query, the success function of which correctly displays the latest data.

    The next stage is to introduce a delay in calling an ajax request via setTimeout , for example:

     var delayTimer; function doSearch(text) { clearTimeout(delayTimer); delayTimer = setTimeout(function() { // Π²Ρ‹Π·ΠΎΠ² ajax }, 1000); } 
    • can make some minimum delay after clicking on the last button before sending the request? - Alexander Sizintsev
    • Yes, that is what really helps. The last example I gave postpones sending a request for 1 second. If you quickly press the keys, the request will be delayed for 1 second since the last press. But it is still better to use both methods together: minimum delay and cancellation of the request. - Petr Kulikov