倒计时计时器?

4
4个回答

10

像这样的吗?

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

6
对于可能会阅读本线程的许多人...您不应该将字符串传递给setTimeout。相反,应该传递一个匿名函数: setTimeout(function() { countDown(count-1); }, 1000); - Rob Flaherty
@rob:请考虑根据RobFlaherty的评论修改您的答案,以改进它。 - ThiefMaster

0

使用纯JavaScript,您可以像这样做:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接