I made a simple live search function to filter results in a table.
See below:
$( "#searchinput" ).keyup(function() {
if ($( "#searchinput" ).val() == ''){
//LEEG
$( "#tabledatadiv" ).html('Loading...');
$( "#tabledatadiv" ).load('tabledata.php');
}else{
//NIET LEEG
$( "#tabledatadiv" ).html('Loading...');
$( "#tabledatadiv" ).load('tabledata.php?searchquery='+encodeURIComponent($( "#searchinput" ).val()));
}
});
The problem is that when a user is typing fast, and the previous "string"my script is searching for has a lot of results, the result of the previous search overwrites the results of the final one.
So, when searching for "ax" which gives 2 results, it first tries (while typing) to search for "a" which has 5000 results.
Loading the result for ax takes less time than loading results for a, to so you the result you need for a short time, and then the content of the div is overwritten by the result for "a".
How to prevent this? When a user types in a searchbox, it should stop loading or displaying the result of the previous keystroke.