网页定时刷新

javascript

浏览数:304

2019-1-8


监听网页的键盘和鼠标事件,长时间无操作则刷新页面

/**
 * 页面刷新
 * @param time 单位为分钟
 */
function timeRefresh(time) {
    var xIndex ;
    var yIndex ;
    var count = 0;
    var outTime=30; //默认30分钟无操作则刷新
    if (time != undefined && time != "" && time != null) {
        outTime=time;
    }
    //监听鼠标
    document.onmousemove = function (event) {
        var xMouse = event.clientX;
        var yMouse = event.clientY;
        if (xIndex != xMouse || yIndex != yMouse) {
            count = 0;  //鼠标移动,计数清0
        }
        xIndex = xMouse; //记录本次鼠标移动位置
        yIndex = yMouse;
    };
    //监听键盘
    document.onkeydown = function () {
        count = 0; //键盘按下,计数清0
    };

    window.setInterval(timeCount, 1000);
    function timeCount() {
        count++;
        if (count == outTime*60) {
            window.location.reload();
        }
    }
}