首先看下前端代碼:
<body style="text-align: center;"> <br/>系統超時或未登錄!<br/><br/> <span id="spanTime" style="color: red">5</span> 秒后自動跳轉到登錄界面,或者<a href='javascript:redirect()'>點擊這里</a>直接登錄! </body>
然后是js代碼:
<script type="text/javascript"> window.onload = function(){ //1.首先聲明seconds var seconds = 4; //2.聲明定時器 var timer = null; //3.開啟定時器 timer = setInterval(show,1000); //4.設置跳轉路徑
var location="<%=request.getContextPath() %>/admin/login/index.do";這里改成需要跳轉的路徑 //開啟定時器后要執行的函數 function redirect(){ parent.parent.parent.parent.location.href = location; } function show(){ if(seconds==0){ clearInterval(timer);//清除定時器 parent.parent.parent.parent.location.href = location; return; } //將不斷變化的秒數顯示在頁面上 document.getElementById('spanTime').innerHTML = seconds; seconds--; } }; </script>
