我无法在2位数内进行小时转换。目前的产出将包括整个小时,它不包括日期。如果你运行它,你会更好地了解它。在控制台中尝试timer(7.543e+7);
。
let countdown;
const timerDisplay=document.querySelector('.time_left');
function timer(seconds){
const now= Date.now();
const then=now+seconds*1000;
displayTimeLeft(seconds);
countdown= setInterval(()=>{
const secondsLeft= Math.round((then-Date.now())/1000);
if(secondsLeft<0){
clearInterval(countdown);
return;
}
displayTimeLeft(secondsLeft);
},1000);
}
function displayTimeLeft(seconds){
const days=Math.floor(seconds/(24*60*60));
const hours=Math.floor(seconds/3600);
const minutes=Math.floor((seconds - (hours * 3600)) / 60);
const remainderSeconds=seconds%60;
const display = `${days}:${hours}:${minutes}:${remainderSeconds < 10 ? '0' : '' }${remainderSeconds}`;
document.title= display;
timerDisplay.textContent= display;
}
输出:enter image description here
输出应该是[873:22:27:53]
。