码迷,mamicode.com
首页 > Web开发 > 详细

web页面超时自动退出方法

时间:2017-05-17 13:44:07      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:ons   判断   当前时间   思路   last   read   事件   get   console   

思路:
使用 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页面超时自动退出方法

标签:ons   判断   当前时间   思路   last   read   事件   get   console   

原文地址:http://www.cnblogs.com/laman-chen/p/6866838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!