最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Fire javascript function 2 sec after no activity in input tag - Stack Overflow

programmeradmin8浏览0评论

Is it possible to check if a user has not typed in a text <input/> tag in more than two seconds using javascript?

<input type="text" onKeyUp="check_last_active()" />

The function should trigger something (an alert) only 2 seconds after the user's last input in the <input/> tag, thereby resetting any previous timer functions.

Is it possible to check if a user has not typed in a text <input/> tag in more than two seconds using javascript?

<input type="text" onKeyUp="check_last_active()" />

The function should trigger something (an alert) only 2 seconds after the user's last input in the <input/> tag, thereby resetting any previous timer functions.

Share Improve this question edited Jan 30, 2014 at 18:53 proPhet asked Jan 30, 2014 at 15:09 proPhetproPhet 1,2284 gold badges21 silver badges39 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10
var timer;
function onInput() {
    clearTimeout(timer);
    timer = setTimeout(functionToRunAfter2Seconds.bind(this), 2000);
}

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

The input event is for input elements and textarea elements (assumed you were using one).

You need to clear the timeout so that if they start typing again you reset the timer.

if you want the jQuery way....

$('#idOfInputOrTextarea').on('input', onInput);
发布评论

评论列表(0)

  1. 暂无评论