js案例:5秒后网页自动跳转location.href

286 阅读1分钟
<!DOCTYPE HTML>
<html>
 <head>
  <title>案例:5秒钟之后自动跳转页面</title>
  <meta charset="utf-8" />
  <style>
  </style>
 </head>
 <body>
  <button id="btn">点击进入</button>
  <script>


   var btn = document.getElementById('btn');
   btn.addEventListener('click', function() {
    var timer = null;
    var num = 5;

    var timer = setInterval(function() {
     if(num == 0) {
     //clearTimeout(timer);
     location.href = 'https://www.bilibili.com/';
     //num = 5;
      }  
       else {
         btn.innerHTML = num + '秒后将会带您进入B站首页';
          num--;
        }  
     }, 1000);
  })
  </script>
 </body>
</html>