<p class="" id="countdown" style="color: #df4529;"></p>
//倒计时
countdown(new Date().getTime() + 15 * 60 * 1000 ,"countdown");
/*
*倒计时
* 目标时间毫秒数(分钟数*60*1000) targetTime
* 倒计时显示的元素id
*/
function countdown(targetTime,DomId) {
let showTime="";
var nowTime = new Date().getTime();
var distance = targetTime - nowTime;
var days = Math.floor(distance / (1000 * 60 * 60 * 24)); //向下取整
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.ceil((distance % (1000 * 60)) / 1000); //向上取整
// 显示倒计时
showTime = minutes + "分钟 " + seconds + "秒 ";
document.getElementById(DomId).innerHTML = showTime;
if (distance > 0) {
setTimeout(function() {
countdown(targetTime,DomId);
}, 1000);
} else {
showTime = "倒计时已结束";
document.getElementById(DomId).innerHTML = showTime;
}
}
JS 倒计时15分钟
于 2023-12-25 11:55:05 首次发布