js根据鼠标和键盘判断页面是否长时间未进行操作

时间:2021-09-08 00:36:08
<script>
    var count = 0;
    var outTime = 1;//分钟
    window.setInterval(go, 1000);

    function go() {
        count++;
        if (count == outTime * 60) {
            // alert("设置时长N分钟无鼠标键盘操作,自动跳转下一个页面");
            window.location.href = "tianjin.html?backurl=" + window.location.href;
        }
    }

    var x;
    var y;
    //监听鼠标
    document.onmousemove = function (event) {
        var x1 = event.clientX;
        var y1 = event.clientY;
        if (x != x1 || y != y1) {
            count = 0;
        }
        x = x1;
        y = y1;
    };
    //监听键盘
    document.onkeydown = function () {
        count = 0;
    };
</script>