js检测web页面长时间不操作跳转到指定页面,如果操作重新计时@TOC
JS 代码
<script type="text/javascript">
var mouse1;//记录鼠标坐标
var mouse2;
var eventID;
function mousePos(e){
var x,y;
var e = e||window.event;
return {
x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,
y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop
};};
function GetXYPosition(e){
document.title = mousePos(e).x+','+mousePos(e).y;//这里是把title修改成为鼠标的横纵坐标
mouse2 = document.title;
};
var timeout = 180*1000; //设定超时时间,单位毫秒
var currentSecond = 0;
function CheckTime(){
if(mouse1 == mouse2){
currentSecond = currentSecond+1000;
if(currentSecond>timeout){
clearInterval(eventID);
alert("你已经超过3分钟没有操作,页面自动跳转至首页");
window.location.href = "http://www.baidu.com";
}
}else{
currentSecond = 0;
mouse1 = mouse2;
}
}
function StartPoint(){
mouse1 = "";
mouse2 = "";
eventID = setInterval(CheckTime,1000);
}
</script>
JSP代码
<body onload="StartPoint()">
<div id="content" onmousemove="GetXYPosition(event)">
<!-- 这里要用这个div把你的代码包住,因为检测鼠标横纵坐标是只能在这个范围之内才可以执行这个代码 -->
</div>
</body>
原作者博客地址:https://blog.csdn.net/zanychou/article/details/8941222