开发者社区> 问答> 正文

关于JS倒计时的问题

倒计时的秒数显示不正常。太慢。点解。。。

<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
  </head>
  
  <body>
      
    <h2>毫秒的倒计时</h2>
    <div id="timer2"></div>
    <script>
      var countdown = function(gid,time){
        try {
            if (time<=0) {
            } else {

                var ms = Math.floor(time%1000);
                var s = Math.floor(time/1000%60);
                var m = Math.floor(time/1000/60%60);
                var h =Math.floor(time/1000/60/60%24);

                h=h>9?h:'0'+h;
                m=m>9?m:'0'+m;
                s=s>9?s:'0'+s;
                ms=ms>9?ms:'0'+ms;
                if (parseInt(h)>0) {
                    var str = h+':'+m+':'+s;
                } else {
                    var str = m+':'+s+':'+ms;
                }
                document.getElementById('timer2').innerHTML = str;
                setTimeout(function(){
                    countdown(gid,time-1);
                },1);
            }
        } catch (e) {
            if (typeof(console) == 'object') {
                console.log(e);
            }
        }
    };
    setTimeout(function(){countdown('timer2',99999)},1);
  
    </script>
      
      </body>
</html>

展开
收起
a123456678 2016-03-11 14:09:08 2004 0
1 条回答
写回答
取消 提交回答
  • <!DOCTYPE html>
    <html>
      
      <head>
        <meta charset="UTF-8">
      </head>
      
      <body>
          
        <h2>毫秒的倒计时</h2>
        <div id="timer2"></div>
        <script>
          var countdown = function(gid,time,starttime){
            if (!starttime) starttime = Date.now();
            try {
                var _time = time + starttime - Date.now();
                if (_time<=0) {
                    document.getElementById('timer2').innerHTML = '00:00:00';
                } else {
                    var ms = Math.floor(_time%1000);
                    var s = Math.floor(_time/1000%60);
                    var m = Math.floor(_time/1000/60%60);
                    var h = Math.floor(_time/1000/60/60%24);
    
                    h=h>9?h:'0'+h;
                    m=m>9?m:'0'+m;
                    s=s>9?s:'0'+s;
                    ms=ms>9?ms:'0'+ms;
                    if (parseInt(h)>0) {
                        var str = h+':'+m+':'+s;
                    } else {
                        var str = m+':'+s+':'+ms;
                    }
                    document.getElementById('timer2').innerHTML = str;
                    
                    
                    setTimeout(function(){
                        countdown(gid, time, starttime);
                    },1);
                }
            } catch (e) {
                if (typeof(console) == 'object') {
                    console.log(e);
                }
            }
        };
        setTimeout(function(){countdown('timer2',99999)},1);
      
        </script>
          
          </body>
    </html>
    2019-07-17 18:59:10
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载