我以某种方式从stackoverflow复制了这段代码,但我忘记了链接。我注意到,如果我尝试输入小时数,这将被转换为分钟,这看起来很奇怪。
var hms = "02:30:00";
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
if(seconds > 0)
{
function secondPassed() {
var minutes = Math.round((seconds - 30)/60),
remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = ":" +minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
//form1 is your form name
document.form_quiz.submit();
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
}
&#13;
time{
color:red;
}
&#13;
02:30:00 <br>
<time id="countdown">02:30:00</time>
&#13;
倒计时后应显示为02:29:59(1秒后)。而不是149:59。
答案 0 :(得分:1)
你忘了计算小时时间。
试试这样==&gt;
var hms = "02:30:00";
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
if(seconds > 0)
{
function secondPassed() {
var minutes = Math.round((seconds - 30)/60),
remainingSeconds = seconds % 60;
var hour =Math.floor((minutes)/60);
minutes = minutes%60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
hour = ("0" + hour).slice(-2);
minutes = ("0" + minutes).slice(-2);
remainingSeconds= ("0" + remainingSeconds).slice(-2);
document.getElementById('countdown').innerHTML = hour +":" +minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
//form1 is your form name
document.form_quiz.submit();
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
}
&#13;
time{
color:red;
}
&#13;
02:30:00 <br>
<time id="countdown">02:30:00</time>
&#13;