欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js简单时分秒倒计时

程序员文章站 2022-07-02 21:22:42
效果: javascript: HTML: ......

效果:

js简单时分秒倒计时

javascript:

    <script type="text/javascript">
        function counttime() {
            //获取当前时间
            var date = new date();
            var now = date.gettime();
            //设置截止时间
            var enddate = new date("2018-10-25 00:00:00");
            var end = enddate.gettime();
            //时间差
            var differtime = end - now;
            //定义变量,h,m,s保存倒计时的时间
            var h, m, s;
            if (differtime >= 0) {
                h = math.floor(differtime / 1000 / 60 / 60);
                m = math.floor(differtime / 1000 / 60 % 60);
                s = math.floor(differtime / 1000 % 60);
                h = h < 10 ? ("0" + h) : h;
                m = m < 10 ? ("0" + m) : m;
                s = s < 10 ? ("0" + s) : s;
                document.getelementbyid("_h").innerhtml = h + "时";
                document.getelementbyid("_m").innerhtml = m + "分";
                document.getelementbyid("_s").innerhtml = s + "秒";
                settimeout(counttime, 1000);

            } else {
                document.getelementbyid("_h").innerhtml = "00时";
                document.getelementbyid("_m").innerhtml = "00分";
                document.getelementbyid("_s").innerhtml = "00秒";
            }


        }
    </script>

html: 

 

<body onload="counttime()">
    <div class="timer">
        <span id="_h"></span>
        <span id="_m"></span>
        <span id="_s"></span>
    </div>
</body>