倒计时,js

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS实现倒计时(时、分,秒)</title>
    <script>
        function ShowCountDown(divname)
        {
            var now = new Date();
            
            var year = now.getFullYear();
            var month = now.getMonth()+1;
            var day = now.getDate();    //获取天数
            var hour = parseInt(now.getHours());
            var minuttes = now.getMinutes();
            
            var end_hour = 10;
            
            if(hour<10){
                end_hour = 10;
            }else if(hour<16){
                end_hour = 16;
            }else if(hour>16){
                day = day + 1;
                end_hour = 10;
            }

            var str = year +"-"+ month + "-" + day +" "+end_hour+ ":00:00";
            var leftTime = Date.parse(str) - now.getTime();
            
            var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10);//计算剩余的小时数
            var mm = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟数
            var ss = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
            hh = checkTime(hh);
            mm = checkTime(mm);
            ss = checkTime(ss);//小于10的话加0
            var cc = document.getElementById(divname);
            cc.innerHTML =  hh + ":" + mm + ":" + ss + "";
        }
        function checkTime(i)
        {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        window.setInterval(function(){ShowCountDown('countdown');}, 1000);
        //window.setTimeout(function(){ShowCountDown('countdown');}, 1000);
    </script>
</head>
<body>
<div id="countdown"></div>
</body>
</html>

 

posted @ 2016-11-05 15:35  天道酬勤,坚持!  阅读(295)  评论(0编辑  收藏  举报