倒计时功能
变量chazi就是倒计时的时间戳,这里是由PHP嵌入的,也可以由ajax调接口取值当做参数使用。
function timeOut() {
var setTimer = null;
var chazhi = 0;
//差值计算
//真实时间(注意月份需减掉1,否则时间会计算错误)
//chazhi = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数
chazhi = (new Date({{$year}},{{$month}}-1,{{$day}},{{$hour}},{{$min}},{{$sec}})) - (new Date());
//执行函数部分
countFunc(chazhi);
console.log(chazhi)
setTimer = setInterval(function() {
chazhi = chazhi - 1000;
countFunc(chazhi);
}, 1000);
function countFunc(leftTime) {
if(leftTime >= 0) {
var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
days = checkTime(days);
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
$(".day").html(days);
$(".hour").html(hours);
$(".minute").html(minutes);
$(".second").html(seconds);
} else {
$('.head_three_img').css('display','block')
$('.down_foot').css('display','none')
clearInterval(setTimer);
$(".day").html("00");
$(".hour").html("00");
$(".minute").html("00");
$(".second").html("00");
}
}
function checkTime(i) { //将0-9的数字前面加上0,例1变为01
if(i < 10) {
i = "0" + i;
}
return i;
}
}
timeOut();
<div class="down_foot">
<div class='mins day'>00</div>
<div class='seobs'>天</div>
<div class='mins hour'>00</div>
<div class='seobs'>:</div>
<div class='mins minute'>00</div>
<div class='seobs'>:</div>
<div class='mins second'>00</div>
</div>