js倒计时

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="box">距离。。。。。。。还有00天00小时00分钟00秒</div>

    <script>
        var date2 = new Date('2018-4-11 09:20:00');
        function change() {
            var date1 = new Date();
            var ms = date2.getTime() - date1.getTime();
            var str;
            if(ms <= 0){
                str = '时间到!';
                clearInterval(timer);
            }else{
                var day = parseInt(ms/1000/60/60/24);
                var hour = parseInt(ms/1000/60/60%24);
                var min = parseInt(ms/1000/60%60);
                var sec = parseInt(ms/1000%60);
                str = '距离。。。。还有'+addZero(day)+'天'+addZero(hour)+'小时'+addZero(min)+'分钟'+addZero(sec)+'秒';
            }
            document.getElementById('box').innerHTML = str;
        }
        var timer = setInterval(change,1000);
        function addZero(a) {
            return a<10? '0'+a : a;
        }
    </script>
</body>
</html>
阅读更多
个人分类: javascript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭