javascript - 分钟增量为 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);
解决方案
//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);
时钟时间();
推荐阅读
- php - 子域内部服务器错误上的 laravel 5.6 此外,遇到 500 内部服务器错误错误
- java - 在 android:onClick 属性的父或祖先上下文中找不到方法
- jquery - Bootstrap Tagsinput + Typeahead refresh 不起作用
- javascript - 变形后伪造查看器模型位置
- google-cloud-platform - 在 GCE 上安装监控代理
- vue.js - Vuetify 数据表搜索不起作用
- python - 在 S3 存储桶之间移动特定文件的 Python 脚本
- mysql - 多列搜索
- javafx - Cell Factory 是否支持两种不同类型的对象?
- jquery - jQuery mobile 停止在同一页面中使用 iFrame - Phonegap