1. 程式人生 > >web頁面超時自動退出方法

web頁面超時自動退出方法

如果 鼠標移動 ready 判斷 web tin 頁面 tint 思路

思路:
使用 mousemover 事件來監測是否有用戶操作頁面,寫一個定時器間隔特定時間檢測是否長時間未操作頁面,如果是,退出;
具體時間代碼如下(js):
var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 10 * 60 * 1000; //設置超時時間: 10分
$(document).ready(function(){
/* 鼠標移動事件 */
$(document).mousemove(function(){
lastTime = new Date().getTime(); //更新操作時間

});
});

function testTime(){
currentTime = new Date().getTime(); //更新當前時間
if(currentTime - lastTime > timeOut){ //判斷是否超時
console.log("超時");
}
}

/* 定時器 間隔1秒檢測是否長時間未操作頁面 */
window.setInterval(testTime, 1000);

如不用jq可修改為對應的js

var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 10 * 60 * 1000; //設置超時時間: 10分

window.onload=function init(){
window.document.onmousemove=(function(){
lastTime = new Date().getTime(); //更新操作時間
}
)};

function testTime(){
currentTime = new Date().getTime(); //更新當前時間
if(currentTime - lastTime > timeOut){ //判斷是否超時
console.log("超時");
}
}

/* 定時器 間隔1秒檢測是否長時間未操作頁面 */
window.setInterval(testTime, 1000);

web頁面超時自動退出方法