日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/2 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 页面监听,一段时间内不操作网页,就自动跳转到登录页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求:用戶在 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】 } } }

注意:

  • window.top 獲取到父頁面的信息,即 index.html 頁,因為 index.html 和 login.html 同級,所以只要把 index.html 的路由信息中的 index 改為 login,就可以跳轉到 login.html頁面。
  • outline.html 是一個中轉站頁面,目的是為了不讓頁面跳轉太過生硬。沒有這個頁面的可以直接把代碼中的 outline.html 改為 login.html;
  • 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>

    總結

    以上是生活随笔為你收集整理的页面监听,一段时间内不操作网页,就自动跳转到登录页的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。