Javascript倒計時
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS實現倒計時(時、分,秒)</title>
<script language="javascript" type="text/javascript">
var interval = 1000;
function ShowCountDown(year,month,day,divname){
//獲取當前時間
var now = new Date();
//結束時間
var endDate = new Date(year, month-1, day);
//結束時間差
var leftTime = endDate.getTime()-now.getTime();
//計算總秒
var leftsecond = parseInt(leftTime/1000);
//計算天
var days=Math.floor(leftsecond/(60*60*24));
//計算小時
var hours=Math.floor((leftsecond-days*24*60*60)/3600);
//計算分
var minutes=Math.floor((leftsecond-days*24*60*60-hours*3600)/60);
//計算秒
var seconds=Math.floor(leftsecond-days*24*60*60-hours*3600-minutes*60);
console.log(days);
var cc = document.getElementById(divname);
var data1=document.getElementById(‘data‘);
var hours1=document.getElementById(‘hours‘);
var minutes1=document.getElementById(‘minutes‘);
var seconds1=document.getElementById(‘seconds‘);
//判斷數字小於10,前面加零
function checkTime(i){
if(i < 10){
i = "0" + i;
}
return i;
}
data1.innerHTML=checkTime(days);
hours1.innerHTML=checkTime(hours);
minutes1.innerHTML=checkTime(minutes);
seconds1.innerHTML=checkTime(seconds);
}
window.setInterval(function(){
ShowCountDown(2020,1,1,‘divdown1‘);
}, interval);
</script>
</head>
<body>
<div id="divdown1">
距結束<span id="data" class="time">00</span> 天 <span id="hours" class="time">00</span> : <span id="minutes" class="time">00</span> : <span id="seconds" class="time">00</span>
</div>
</body>
</html>
Javascript倒計時