3 ms·
Capture the timeoutId in a closure. $(document).ready((function() { $('input').keypress((function() { var timeoutId = null; return funct
by dmbass 13y ago
Capture the timeoutId in a closure.
$(document).ready((function() {
$('input').keypress((function() {
var timeoutId = null;
return function() {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(function() {
$.ajax({ /* ... * /});
}, 200);
};
})());
});
// NB: this solution only works if there is one input.
// If there are more than one, you need to create a new closure for each one.