js检测web页面长时间不操作跳转到指定页面,如果操作重新计时

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值