每次击键后重置的计时器上的事件?Javascript。

每次击键后重置的计时器上的事件?Javascript。,javascript,timer,keystroke,repeat,Javascript,Timer,Keystroke,Repeat,聊天室中出现10秒未键入后试图触发蜂鸣声 考虑构建一个10秒的定时器,该定时器在每次击键时重置。有什么想法吗?我想你期望的解决方案与此类似 代码: var timer; $('#tArea').keyup(function(){ clearTimeout(timer); timer = setTimeout(yourAction, 10000); // interval is set to 10s }); $('#tArea').keydown(function(){

聊天室中出现10秒未键入后试图触发蜂鸣声


考虑构建一个10秒的定时器,该定时器在每次击键时重置。有什么想法吗?

我想你期望的解决方案与此类似

代码:

var timer; 

$('#tArea').keyup(function(){
    clearTimeout(timer);
    timer = setTimeout(yourAction, 10000); // interval is set to 10s
});

$('#tArea').keydown(function(){
    clearTimeout(timer);
});

function yourAction () {
    // Your code goes here
    alert("You have not typed anything for the past 10 seconds!!");
}
检查