1、倒计时
倒计时
1、首先需要知道当前时间采用new Date();
活动倒计时 = (结束的时间 - 现在的时间)/1000; 转换成秒
同时,不能忘记如果不够两位数的时候进行拼接字符串,实现时间的格式化
2、本次的收获:数组转换字符串,从字符串获取到每一个字符。
<body>
<p class="count"></p>
<script>
window.onload = function () {
countDown();
function addZero(i) {
return i < 10 ? "0" + i : i + "";
}
function countDown() {
var nowtime = new Date();
var endtime = new Date("2019/03/16,17:57:00");
var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
var d = parseInt(lefttime / (24 * 60 * 60))
var h = parseInt(lefttime / (60 * 60) % 24);
var m = parseInt(lefttime / 60 % 60);
var s = parseInt(lefttime % 60);
d = addZero(d)
h = addZero(h);
m = addZero(m);
s = addZero(s);
var time = [h, m, s];
var times = time.join();
console.log(times);
hoursT.innerHTML = times.charAt(0);
hoursB.innerHTML = times.charAt(1);
minutesT.innerHTML = times.charAt(3);
minutesB.innerHTML = times.charAt(4);
secondsT.innerHTML = times.charAt(6);
secondsB.innerHTML = times.charAt(7);
document.querySelector(".count").innerHTML = `活动倒计时 ${d}天 ${h} 时 ${m} 分 ${s} 秒`;
if (lefttime <= 0) {
document.querySelector(".count").innerHTML = "活动已结束";
return;
}
setTimeout(countDown, 1000);
}
}
</script>
</body>