js时分秒倒计时代码

以下是JS时分秒倒计时案例,大家可以直接拿去使用或参考

demo:

<html>
<head>
    <title>时分秒倒计时</title>
    <script type="text/javascript">
 
        var currentDate;//当前时间
        var endtime;//结束时间
        var days;
        var ms;//精确到秒
 
        function timer(obj) {
            h = Math.floor(ms / 60 / 60);
            m = Math.floor((ms - h * 60 * 60) / 60);
            s = Math.floor((ms - h * 60 * 60 - m * 60));
            d = parseInt(h / 24);
 
            h = prefix(h, 2);
            m = prefix(m, 2);
            s = prefix(s, 2)
 
            var time_val = "";
            switch (obj) {
                case 1://模式一:00:00:00 时分秒
                    time_val = h + ":" + m + ":" + s;
                    break;
                case 2://模式二:00:00 分秒
                    time_val = m + ":" + s;
                    break;
            }
            document.getElementById("timer").innerHTML = time_val;
 
            ms--;
            if ((ms) < 0) {
                document.getElementById("timer").innerHTML = "已过期";
            }
 
        }
 
        function getQrcode() {
            currentDate = new Date();//当前时间
            endtime = new Date(currentDate.getTime() + (1000 * 60 * 1));//结束时间
            days = endtime - currentDate;
            ms = parseInt(days / 1000);//精确到秒}
        }
 
        function reload() {
            location = location;
        }
 
        //num传入的数字,n需要的字符长度
        function prefix(num, n) {
            var len = num.toString().length;
            while (len < n) {
                num = "0" + num;
                len++;
            }
            return num;
        }
 
        window.onload = function () {
            getQrcode();
            setInterval("timer(2)", 1000);
        }
    </script>
</head>
<body>
    <script>document.write(new Date().getTime());</script>
    <br />
    <a href="javascript:void();" οnclick="reload()">刷新</a>
    <div id="timer"></div>
</body>
</html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.duanlonglong.com/qdjy/564.html