JS倒计时精确到毫秒

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>倒计时精确到毫秒</title>
    <style>
    #timer {
        height:50px;
        width:100%;
        text-align:center;
        line-height:50px;
        background:black;
        color:white;
    }
    </style>
    <script>
    function countDown(dd) {
        var t = new Date(dd),
        n = new Date().getTime(),
        c = t - n;

        // 结束执行
        if (c <= 0) {
            clearInterval(window['timer']);
            return;
        }

        d = parseInt(c / 1000 / 60 / 60 / 24),
        h = parseInt(c / 1000 / 60 / 60 - (24 * d)),
        m = parseInt(c / 1000 / 60 - (24 * 60 * d) - (60 * h)),
        s = parseInt(c / 1000 - (24 * 60 * 60 * d) - (60 * 60 * h) - (60 * m));
        hm = Math.floor((c - (24 * 60 * 60 * 1000 * d) - (60 * 60 * 1000 * h) - (60 * 1000 * m) - (s * 1000)) / 10);

        if (hm < 10) {hm = "0" + hm;}
        if (s < 10) {s = "0" + s;}
        if (m < 10) {m = "0" + m;}
        if (h < 10) {h = "0" + h;}

        document.getElementById('timer').innerHTML = '距离2118年10月1日还有:' + d + '' + h + '小时' + m + '分钟' + s + '' + hm + '毫秒';
    }
    (function() {
        window['timer'] = setInterval(function() {
            countDown('2118/10/1 00:00:00:00');
        }, 50);
    })();
    </script>
</head>
<body>
    <div id="timer"></div>
</body>
</html>
原文地址:https://www.cnblogs.com/chenjiacheng/p/7228571.html