页面监听,一段时间内不操作网页,就自动跳转到登录页

本文介绍如何使用jQuery在多个页面上实现用户在5分钟无操作后自动转向登录页的功能,包括设置定时器检测用户活动,以及利用window.top定位跳转路径。

需求:用户在 5 分钟内没有操作网页,就自动跳转到登录页。

环境:jquery 项目,有公共的 js 文件 。

在所有页面都引用的 js 文件中添加下面代码:

//判断用户是否在5分钟内未操作页面,如果没有操作,则跳转到登录页
var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 5*60*1000; //设置超时时间: 5分钟
$(function(){
  /* 鼠标移动事件 */
  $(document).mouseover(function(){
    lastTime = new Date().getTime(); //更新操作时间
  });
});
/* 定时器  间隔1秒检测是否长时间未操作页面  */
var timer = window.setInterval(testTime, 1000);
function testTime(){
  currentTime = new Date().getTime(); //更新当前时间
	if(location.href.substring(location.href.length-10) == "login.html"){ //登录页
      clearInterval(timer);  //关闭定时器
	  console.log("当前所在页为登录页,不需要跳转");
	} else {
	  if(currentTime - lastTime > timeOut){ //判断是否超时---超时
		clearInterval(timer); 
		var src = window.top.location.href.substring(0, location.href.length-10); //【注1】
        //跳转到outline,提示用户跳转,可在后台进行销毁session;
	    location.href = src + "outline.html"; //没有这个页面的可以直接跳转到登录页【注2】	  
      } 
	}		
}

注意:

  1. window.top 获取到父页面的信息,即 index.html 页,因为 index.html 和 login.html 同级,所以只要把 index.html 的路由信息中的 index 改为 login,就可以跳转到 login.html页面。
  2. outline.html 是一个中转站页面,目的是为了不让页面跳转太过生硬。没有这个页面的可以直接把代码中的 outline.html 改为 login.html;
  3. substring(start, end); 方法用来截取字符串,返回一个新字符串,不影响原数组。start 是开始位置(从0开始),end 表示结束位置。

 outline.html


<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<style type="text/css">
* {margin: 0;padding: 0;}
/*手机*/
@media screen and (max-width: 600px) {
	.mBody .fresh { position: absolute; left: 35%; top: 70%; width: 30%; }
}
/*平板*/
@media screen and (min-width: 600px) and (max-width: 960px) {
	.mBody .fresh { position: absolute; left: 35%; top: 78%; width: 30%; }
}
/*电脑*/
.pcBody { background: url(images/tips/bg.png);background-position-y: -150px; width: 100%; height: 100%; overflow: hidden; }

.pcContent {width: 700px;margin: 265px auto;}
.pcContent h1 {font-size: 36px;color: #cb9f63;line-height: 60px;}
.pcContent p {font-size: 18px;color: #66686A;line-height: 24px;}
.pcContent p a {color: #cb9f63;}
</style>

<body>
	<div class="pcContent">
		<h1>由于您长时间未操作,请重新登录!</h1>
		<p style="text-align: center;">
			<span id='second' style="color: red; font-size: 20px;"></span>s后,您将返回<a href="../login.html">登录</a>页面。
		</p>
	</div>
</body>

<script src="plugins/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>

<script>
  (function(){
    var system ={};
    var p = navigator.platform;       
    system.win = p.indexOf("Win") == 0;  
    system.mac = p.indexOf("Mac") == 0;  
    system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);     
    if(system.win||system.mac||system.xll){//如果是电脑
      document.getElementsByTagName("body")[0].className="pcBody";    
    }else{  //如果是手机或平板
        var body=document.getElementsByTagName("body")[0];
      body.className="mBody";
        body.innerHTML='<img class="fresh" src="../common/tips/fresh.png" onclick="history.go(0)">';
    }
  })();

	var count=3; //3秒钟后跳转到登录页
	$("#second").text(count+'');
	 var timer = setInterval(go,1000);
	  function go(){
	    count--;
	    if(count==0) {
        clearInterval(timer);
		    window.location.href="login.html";
	    }else{
	    	$("#second").text(count+'');
	    }
	  }
	</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值