i have a dynamic search in php with jquery. When i'm entering a letter, an ajax call starts. My problem is, that all ajax calls are working till end, so that every letter is a full call. When a user is entering a full word then i have unused requests. How can i stop the unused calls?
Thank you very much
One way you could do it , is to send the request when the user presses the return key , or atleast wait a few seconds ( 1-3 ) to see if the user stoped typing then make the request , you don't have to do the search for all changes on the input box .
Either use a button to process the request that the user has to click when their finished or perhaps use something like debouncing.
Use timeouts to delay the request for a few millisecond, and clear it when a new key is pressed.
something like
var searchtimeout;
$('#search').keyup( function(){
if (searchtimeout)
{
clearTimeout(searchtimeout);
}
searchtimeout = setTimeout(function(){
// initiate the ajax call here..
}, 300);
} );
Ok, here is my solution:
if (searchtimeout)
{
clearTimeout(searchtimeout);
}
searchtimeout = setTimeout(function(){
if(x){
x.abort();
alert("MACH WEG!");
}
x = $.post(url + "ajax.html?nsearch=1&ckey="+SID, {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
$('#suggestions').fadeIn(); // Show the suggestions box
$('#suggestions').html(data); // Fill the suggestions box
x = null;
});
}, 300);
Why dont you use the autocomplete plugin in jquery.. I am assuming you have handcoded the dynamic lookup.. auto complete takes care of most of these things and it is also configurable as to after how many letters you want to post to the server..it also implements caching.
I have used it in a lot of asp.net projects and it is pretty neat.. docs.jquery.com/Plugins/autocomplete