web頁面超時自動退出方法


思路:
使用 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 (){
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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM