js实现倒计时
js实现倒计时的代码:
时间展示
function timeGoOn(){ function getFullStr(i){ return i<10?"0"+i:i; } var today = new Date(); var h = getFullStr(today.getHours()); //获取当前小时数(0-23) var m = getFullStr(today.getMinutes()); //获取当前小时数(0-23) var s = getFullStr(today.getSeconds()); //获取当前分钟数(0-59) //$('#timeinfo').text(h+':'+m+':'+s); document.getElementById('h').innerHTML = h; } setInterval(timeGoOn, 500);
倒计时,下面是,html+js:
<div class="head">OPS系统升级倒计时 ......</div> <div class="mc" style="font-size: 40px;color: #828282;"> <span id="time_hour" class="hour" style="color:#DD2A2E">11</span>: <span id="time_minute" class="minute" style="color:#DD2A2E">08</span>: <span id="time_second" class="second">47</span> </div> <script type = "text/javascript"> function timer(){ var ts = (new Date(2014, 6, 9, 20, 0, 0)) - (new Date());//计算剩余的毫秒数 var hh = parseInt(ts / 1000 / 60 / 60 % 24, 10);//计算剩余的小时数 var mm = parseInt(ts / 1000 / 60 % 60, 10);//计算剩余的分钟数 var ss = parseInt(ts / 1000 % 60, 10);//计算剩余的秒数 hh = checkTime(hh); mm = checkTime(mm); ss = checkTime(ss); document.getElementById("time_hour").innerHTML = hh; document.getElementById("time_minute").innerHTML = mm; document.getElementById("time_second").innerHTML = ss; setInterval("timer()",1000); } function checkTime(i){ if (i < 10) { i = "0" + i; } return i; } </script>