我正在尝试增加30分钟的时间,但显示不正确,请帮助我。我的代码中的错误在哪里?
分钟显示像一个垃圾值...
<div id="time"></div>
<script>
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.setMinutes(today.getMinutes()+30);//**here I am adding plus 30 mint but not showing correct**
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
//Minutes display like garbage value....
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function() {
startTime()
}, 500);
}
startTime();
</script>
请查看我的代码并更正。
答案 0 :(得分:-1)
您要嵌套两个不同的函数,我不知道为什么。
此行:
var m = today.setMinutes(today.getMinutes()+30);
应该是:
var m = today.getMinutes()+30
如果运行代码段,它将起作用。
<div id="time"></div>
<script>
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date();
var h = today.getHours();
// CORRECTION HERE
var m = today.getMinutes()+30;
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
//Minutes display like garbage value....
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function() {
startTime()
}, 500);
}
startTime();
</script>