用js實現動態顯示指定時間的倒計時並倒計時完畢后跳轉


 首先看下前端代碼: 

   

<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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM