I have a JavaScript searchbox/filter on my page:
$("#show_search").on("keyup search input paste cut", function() {
send_data["search"] = this.value;
getAPIData();
});
It filters the results I show on the page based on the search term put into the input box. But sometimes, usually when I type a search term very quickly, it doesn't capture the final state of the input text and instead picks up an earlier state.
So, for example, if I search for "miranda", my ajax calls look like this:
[23/Oct/2020 16:12:51] "GET /api/shows/?year=&show_type=&sort_by=name&search=mir&format=json HTTP/1.1" 200 2486
[23/Oct/2020 16:12:51] "GET /api/shows/?year=&show_type=&sort_by=name&search=mira&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:51] "GET /api/shows/?year=&show_type=&sort_by=name&search=mir&format=json HTTP/1.1" 200 2486
[23/Oct/2020 16:12:51] "GET /api/shows/?year=&show_type=&sort_by=name&search=miran&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=mira&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=mirand&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=miranda&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=miran&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=mir&format=json HTTP/1.1" 200 2486
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=miranda&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=mirand&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=miranda&format=json HTTP/1.1" 200 1705
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=mi&format=json HTTP/1.1" 200 40981
[23/Oct/2020 16:12:52] "GET /api/shows/?year=&show_type=&sort_by=name&search=m&format=json HTTP/1.1" 200 41785
You can see that the full search-term does show in that list, but it ends on filtering on "m" when "miranda" is in the search box, which is nowhere close to what I want. What gives? How can I ensure that the current input value will always be extracted from this.value
and stored in send_data["search"]
?