【问题标题】:Countdown timer?倒计时器?
【发布时间】:2011-02-19 13:49:12
【问题描述】:

如何制作倒数计时器?

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

找到这个,它不是太有用。 http://encosia.com/2007/07/25/display-data-updates-in-real-time-with-ajax/

【问题讨论】:

    标签: c# asp.net javascript ajax


    【解决方案1】:

    这样的?

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

    【讨论】:

    • 对于可能最终阅读此线程的许多人...您不想将字符串传递给 setTimeout。而是传递一个匿名函数:setTimeout(function() { countDown(count-1); }, 1000);
    • @rob:考虑根据 RobFlaherty 的评论编辑您的答案以改进它。
    【解决方案2】:
    <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>
    

    【讨论】:

      【解决方案3】:

      使用纯 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
      }
      

      【讨论】:

        【解决方案4】:

        可能最简单的做法是使用Timer class

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多