2014-01-30 31 views

回答

9
var timer; 
function onInput() { 
    clearTimeout(timer); 
    timer = setTimeout(functionToRunAfter2Seconds.bind(this), 2000); 
} 

inputElement.addEventListener('input', onInput, false); 

輸入事件是用於輸入元件和textarea元素(你正在使用一個假定的)。

您需要清除超時時間,以便在他們再次開始輸入時重置計時器。如果你想jQuery的方式

....

$('#idOfInputOrTextarea').on('input', onInput); 
相關問題