0

I have implemented a little "live-search" on a site using jquerys ajax-function. Works most of the time perfectly. Sometimes the user types the search-word too fast and the results are mixed up. For example typing "google" results in a list containing all results with the word "googl", because the answer from "googl" came after the answer from "google".

$("#search").keyup(function () {
var search = $(this).val();
console.log(search);
$.ajax({
    url: "index.php",
    type: "GET",
    data: { 
        s: search,
    },
    cache: false,
    success: function(html, status, xhttp) {
        $(".list").html(html);
        console.log("result for "+search);
    },
});
});

I can not use the parameter "async", because it is deprecated.

Are there any mechanisms to show the answers in the right sequence? Do I have to add some delay while typing the word?

Regards Jens

  • [More debounce answers](https://www.google.com/search?q=debounce+ajax+2020+site:stackoverflow.com) – mplungjan Nov 17 '20 at 10:17
  • and use `$("#search").on("input",function () {` – mplungjan Nov 17 '20 at 10:18
  • Though it doesn't answer my question, because I'm loosing the nice behaviour of showing results while typing fast, I can accept this answers. Unfortunatly I have to add another library to my code... – Jens Körte Nov 17 '20 at 10:59
  • So you need [a queue](https://stackoverflow.com/questions/4785724/queue-ajax-requests-using-jquery-queue) – mplungjan Nov 17 '20 at 11:10

0 Answers0