倒计时器?

时间:2010-05-14 14:54:59

标签: c# asp.net javascript ajax

你如何制作倒数计时器?

当用户加载页面时,时钟开始倒计时,达到时间,它会将浏览器重定向到新页面。

发现这一点,它并没有太大用处。 http://encosia.com/2007/07/25/display-data-updates-in-real-time-with-ajax/

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

最简单的事情可能是使用Timer class

答案 2 :(得分: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
}

答案 3 :(得分: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>