Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查自上次按键以来的时间_Javascript_Html - Fatal编程技术网

Javascript 检查自上次按键以来的时间

Javascript 检查自上次按键以来的时间,javascript,html,Javascript,Html,我正在制作一个脚本,如果在15秒内没有按下某个键,则需要能够重定向某人,唯一的问题是我不知道如何检查自上次按下键以来已过了多少时间 我知道键盘事件,但它们等待输入,而不是检查最后一次输入是何时收到的。这是一个相当简单的实现,但应该为您提供有关如何构建自己的键盘事件的指导: let超时; 常量handleKeyUp=(ev)=>{ clearTimeout(超时); timeout=setTimeout(()=>{location.href='//google.com'},15e3); } wi

我正在制作一个脚本,如果在15秒内没有按下某个键,则需要能够重定向某人,唯一的问题是我不知道如何检查自上次按下键以来已过了多少时间


我知道键盘事件,但它们等待输入,而不是检查最后一次输入是何时收到的。

这是一个相当简单的实现,但应该为您提供有关如何构建自己的键盘事件的指导:

let超时;
常量handleKeyUp=(ev)=>{
clearTimeout(超时);
timeout=setTimeout(()=>{location.href='//google.com'},15e3);
}
window.addEventListener('keyup',handleKeyUp)
let超时;
$(窗口)。按键(功能(e){
如果(超时){
clearTimeout(超时);
超时=空;
}
timeout=setTimeout(()=>console.log(e.key),1500)
//1.5秒,以便更快地看到效果
});

试试这样的方法:

HTML:


Javascript:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script>
    function inactivitiy(){
        var lastTime = $('#lastKeyPressTime').val();
        lastTime++;
        $('#lastKeyPressTime').val(lastTime);
        if(lastTime>15){
            //alert('time to redirect !');
            window.location = "http://www.example.com";
        }
        setTimeout(function(){
            inactivitiy();
        }, 1000);
    }
    $(document).ready(function(){
        $('#input').on('keypress', function(){
            $('#lastKeyPressTime').val(0);
        });

        inactivitiy();

    });
</script>

函数无效(){
var lastTime=$('#lastKeyPressTime').val();
lastTime++;
$('lastKeyPressTime').val(lastTime);
如果(上次时间>15){
//警报(“重定向时间!”);
window.location=”http://www.example.com";
}
setTimeout(函数(){
不活跃();
}, 1000);
}
$(文档).ready(函数(){
$('#input')。on('keypress',function(){
$('lastKeyPressTime').val(0);
});
不活跃();
});

这应该可以做到这一点

在按键事件中添加一个事件监听器,存储某个地方发生的时间(我使用了localstorage,因为如果在同一个域上,它可以用于交叉表),然后使用
setInterval
检查该时间与现在的时间。您也可以只执行脏变量并执行一个简单的
setTimeout(()=>{}, 15000);这很好,在清除它之前,我们是否还需要检查
timeout
是否有值,或者内部处理该值?我喜欢这个答案,但它有点过度工程化。一点也不,我一直使用隐藏类型的输入,它们使事情变得更容易^^精彩,所有答案都很好,但我最理解这个答案。感谢大家的帮助。根本没有理由使用隐藏的输入,只会对性能产生负面影响,因为它必须读取并直接访问DOM。为什么不使用变量呢?
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script>
    function inactivitiy(){
        var lastTime = $('#lastKeyPressTime').val();
        lastTime++;
        $('#lastKeyPressTime').val(lastTime);
        if(lastTime>15){
            //alert('time to redirect !');
            window.location = "http://www.example.com";
        }
        setTimeout(function(){
            inactivitiy();
        }, 1000);
    }
    $(document).ready(function(){
        $('#input').on('keypress', function(){
            $('#lastKeyPressTime').val(0);
        });

        inactivitiy();

    });
</script>