警报窗口,如果没有keydown秒数?

lis*_*aro 3 javascript jquery

我需要知道10秒内是否没有键盘或点击,如果是这样,则显示警告窗口,如果它比简单的js更有效,我可以使用jquery.

我该怎么做?谢谢

Pau*_*aul 5

像这样的东西:

(function(){

    var timer;
    function resetTimer(){
        clearTimeout(timer);
        timer = setTimeout(function(){
            alert('No keydown for 10 seconds');   
            // Call reset timer here if you want the timer to start again
            // when the alert is closed

            // Otherwise, if you want to stop the timer forever,
            // remove the event handler from document.onkeydown
        }, 10000);
    }
    resetTimer();

    document.onkeydown = resetTimer;

})();
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

它假设您没有将任何其他内容绑定到文件keydown侦听器.如果你有,那么你应该使用addEventListenerattachEvent不是.