I\'m using the following code (written by another user) to throttle ajax requests in a livesearch function:
JSFiddle if you prefer a demo: http://jsfiddle.net/4xLVp/
I would just use setTimeout
:
(function() {
var timeout;
$('#tag-search').keyup( function() {
var elem = $(this);
if (elem.val().length >= 2) {
clearTimeout(timeout);
timeout = setTimeout(function() {
$.ajax({ // ajax stuff
'success': function(data){ /*show result*/ }
});
}, 80); // <-- choose some sensible value here
} else if (string.length <= 1) { /*show original content*/ }
});
}());
There is also a debounce/throttle plugin.