Countdown timer?

前端 未结 4 993
暗喜
暗喜 2021-01-03 05:21

How do you make a Countdown timer?

When the user loads the page, clock starts counting down, it reaches time, it redirects browser to a new page.

Found this,

相关标签:
4条回答
  • 2021-01-03 05:22

    with pure javascript you could do it like this

    window.onload=function(){ // makes sure the dom is ready
      setTimeout('function(){document.location = "http://www.google.com"}', 10000) // redirects you to google after 10 seconds
    }
    
    0 讨论(0)
  • 2021-01-03 05:23

    Probably the easiest thing to do would be to use the Timer class.

    0 讨论(0)
  • 2021-01-03 05:35

    Something like this?

       <div id="countDiv"></div>
    
        <script>
        function countDown (count) {
          if (count > 0) {
           var d = document.getElementById("countDiv");
           d.innerHTML = count;
           setTimeout (function() { countDown(count-1); }, 1000);
           }
          else
           document.location = "someotherpage.html";
        }
        countDown(5);
        </script>
    
    0 讨论(0)
  • 2021-01-03 05:44
    <p>
        When this counter reaches 0, you will be redirected to
        <a href="http://path.to.wherever/" id="redirectme">wherever</a>.
        <span id="counter">10</span>
    </p>
    <script type="text/javascript">
    (function(){
    
        var
            counter = document.getElementById("counter"),
            count = parseInt(counter.innerHTML),
            url = document.getElementById("redirectme").href,
            timer;
    
        function countdown() {
            count -= 1;
            counter.innerHTML = count;
            if (count <= 0) {
                clearTimeout(timer);
                window.location.assign(url);
            }
        }
    
        timer = setInterval(countdown, 1000); // 1000 ms
    
    })();
    </script>
    
    0 讨论(0)
提交回复
热议问题