首页 > 解决方案 > 分钟增量为 60 分钟而不是 00

问题描述

我试图在时钟中显示下一整分钟。当我在显示屏上增加分钟时min++显示 60 分钟,然后在 1 秒后更改小时。

我该如何改变它?

function getClockTime_2() {
  let date = new Date();

  let hr = date.getHours();
  let min = date.getMinutes();
  min++ // causes the min to go to 60 and change the hour one second later.

  hr = ("0" + hr).slice(-2);
  min = ("0" + min).slice(-2);
  
  let clock24 = document.getElementById("clockDiv2");
  
  clock24.innerHTML = `${hr}:${min}:00`;
  
}
setInterval(getClockTime_2, 1000);

标签: javascripthtml

解决方案


//Get the current time

函数 getTime() { 让 todaDate = new Date();

todaDate.setMinutes(todaDate.getMinutes() + 1);
let hour = todaDate.getHours();
let minute = todaDate.getMinutes();
let second = todaDate.getSeconds();

hour = ("0" + hour).slice(-2);
minute = ("0" + minute).slice(-2);
second = ("0" + second).slice(-2);


let clock = hour + ":" + minute + ":" + second;

return clock;

}

//Set current time in HTML before call setInterval method.

功能时钟时间(){

str = getTime();
document.getElementById("clockDiv2").innerHTML = str;

}

function refreshTime() {

str = getTime();
document.getElementById("clockDiv2").innerHTML = str;

}

setInterval(refreshTime, 1000);

时钟时间();


推荐阅读