js 倒计时实现

时间:2022-03-19 20:41:09


<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
 
</head> 
 
<body> 
 
<DIV id="CountMsg" class="HotDate"> 
<span id="t_d">00</span> 
<span id="t_h">00</span> 
<span id="t_m">00</span> 
<span id="t_s">00</span> 
</DIV> 
<script type="text/javascript">
var EndTime= new Date(); //获取当前时间
var b = 1;
    EndTime.setMinutes(EndTime.getMinutes() + b, EndTime.getSeconds(), 0);//截止时间

function getRTime(){ //倒计时函数
var NowTime = new Date();//获取当前时间
var t =EndTime.getTime() - NowTime.getTime()//剩余时间,1=1000毫秒
 
var d=Math.floor(t/1000/60/60/24); 
var h=Math.floor(t/1000/60/60%24); 
var m=Math.floor(t/1000/60%60); 
var s=Math.floor(t/1000%60); 
 
document.getElementById("t_d").innerHTML = t + "";
document.getElementById("t_h").innerHTML = h + ""; 
document.getElementById("t_m").innerHTML = m + ""; 
document.getElementById("t_s").innerHTML = s + "";

if(t<0){
document.getElementById("t_d").innerHTML = 3 + "";
}

} 
setInterval(getRTime,1000);//每秒运行一次倒计时函数
</script> 
</body> 
</html>