页面监听,一段时间内不操作网页,就自动跳转到登录页
生活随笔
收集整理的這篇文章主要介紹了
页面监听,一段时间内不操作网页,就自动跳转到登录页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求:用戶在 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】 } } }注意:
?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>總結
以上是生活随笔為你收集整理的页面监听,一段时间内不操作网页,就自动跳转到登录页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue slot的使用
- 下一篇: 小程序 循环中有多个input,怎么获取