jquery keyboard pause catch
Am catching the keyup events which makes calls for each keyup.
jQuery('#Search').keyup(function() {
});
But i need something like when user continuously types characters and开发者_运维技巧 gave a pass at 5th character or in n'th character that time only i want to trigger a ajax call. Here we can use timeout function to catch the event but each keypress will call the timeout function so again the same thing happening
And if user again started typing after a pause then again it will have to trigger a event/ajax call but after a certain pause only.
Here we need to abort the previous events or ajax calls since again user started searching.
So i used .abort();
function to abort the previous calls.
Is there a better way to catch event at after a certain period of time in jquery. Please advice me,
Thanks in advance
var search = $('#Search'),
search_delay = 100;
search.keyup(function() {
var request = search.data('request');
if(request) { // if request is running - abort it
request.abort();
search.data('request', null);
}
clearTimeout(search.data('timer')); // if timer is running - stop it
search.data('timer', setTimeout(function(){
// add your ajax call here or pass a reference
// to the function that will handle the ajax call
search.data('request', $.ajax(...));
}, search_delay);
});
You can use jQ data
API to store references to the intrduced in jQuery 1.5 jqXHR object to be able to abort
the request and to the timer so you can stop the timer with clearTimeout
精彩评论