jq倒计时 天、时、分、秒
这里后端是直接返回的每个倒计时总秒数
$(function(){
$('.countdown').each(function () { //countdown是显示时间元素
let setTimer = null;
let Time = $(this).attr('time'); //获取每个倒计时的总秒数
let obj = $(this);
console.log(Time);
setTimer = setInterval(function () {
Time--;
timer(Time, setTimer, obj);
}, 1000);
})
})
function timer(Time, setTimer,obj) {
if (Time > 0) {
let int = null;
let $time = obj;
Time--;
console.log(Time);
letdays = parseInt((Time / 3600) / 24); //计算剩余的天数
let hours = parseInt((Time / 3600) % 24); //计算剩余的小时
let minutes = parseInt((Time / 60) % 60); // 分
let seconds = parseInt((Time % 60)); // 秒
function checkTime(i) { //将0-9的数字前面加上0\. 1变为01
if (i < 10) {
return '0' + i;
} else {
return i;
}
}
days = checkTime(days);
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
$time.html(`${days}:${hours}:${minutes}:${seconds}`);
} else {
clearInterval(setTimer);
let $time = $('.countdown').html(`活动结束`);
}
}