你可以通過你自己寫去抖方法:
var timer = null;
searchInput.addEventListener('keyup', function (evt) {
clearTimeout(timer);
timer = setTimeout(function() {
// this event listener will postpone its execution until after 1 second have elapsed since the last time it was invoked
// send your ajax request here
}, 1000);
}, false);
或者您可以使用underscore限制keyup事件:
searchInput.addEventListener('keyup', _.debounce(function (evt) {
// this event listener will postpone its execution until after 1 second have elapsed since the last time it was invoked
}, 1000), false);
請不要把兩個不相干的查詢,在一個問題。 – Alnitak 2012-04-27 13:46:58
[等待函數直到用戶停止輸入]的可能的重複(http://stackoverflow.com/questions/10124114/wait-for-function-till-user-stops-typing) – Alnitak 2012-04-27 13:47:52