距离某个时间点的倒计时。
// 距离今晚24:0:0的倒计时
var endYear=new Date().getFullYear();
var endMonth=new Date().getMonth()+1;
var endDay=new Date().getDate();
var endTime=new Date(endYear,endMonth,endDay);
function leftDiv (){
var Time = endTime-(new Date().getTime());
var Hours = addNumber(Math.floor(Time/1000/60/60%24));
var Minutes = addNumber(Math.floor(Time/1000/60%60));
var Seconds = addNumber(Math.floor(Time/1000%60));
if(Time>0){
setTimeout(function(){
leftDiv();
},1000);
}else{
clearTimeout(leftDiv())
}
document.getElementById("divTime").innerText="距离截至日期还有"+Hours+"时"+Minutes+"分"+Seconds+"秒";
}
leftDiv();
function addNumber(num){
var num=(num>9)?num:('0'+num);
return num;
}
固定时间点的倒计时
var intDiff = parseInt(500000); //倒计时总秒数量
function timer(intDiff) {
window.setInterval(function() {
var day = 0,
hour = 0,
minute = 0,
second = 0; //时间默认值
if (intDiff > 0) {
day = Math.floor(intDiff / (60 * 60 * 24));
hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
}
if (minute <= 9) minute = '0' + minute;
if (second <= 9) second = '0' + second;
$('#day_show').html(day + "天");
$('#hour_show').html('<s id="h"></s>' + hour + '时');
$('#minute_show').html('<s></s>' + minute + '分');
$('#second_show').html('<s></s>' + second + '秒');
intDiff--;
}, 1000);
}
$(function() {
timer(intDiff);
});
<div id="countdown">还剩 30 分 00 秒</div>
window.οnlοad=function(){
var countdown=document.getElementById("countdown");
var time=1800;//30分钟换算成1800秒
setInterval(function(){
time=time-1;
var minute=parseInt(time/60);
var second=parseInt(time%60);
countdown.innerHTML='还剩'+minute+'分'+second+'秒';
},1000);
}