如何在分钟和秒中显示 2 位数字并删除计时器旁边的文本?

How do I make appear 2 digits in minutes and seconds and remove text that is beside the timer?

我正在尝试用 JavaScript 制作一个倒数计时器,但有两件事我无法更改,即本应显示为“01:00”的两位数字,但它却出现了比如这个“1:9”,还有一个在计时器旁边显示的短语“This is only valid for the next”。我正在尝试删除它,但失败了。

<html>
  <head>
 <title>Countdown</title>
 <script type="text/javascript">
 // set minutes
var mins = 20;

// calculate the seconds (don't change this! unless time progresses at a           different speed for you...)
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
 function Decrement() {
if (document.getElementById) {
    minutes = document.getElementById("minutes");
    seconds = document.getElementById("seconds");
    // if less than a minute remaining
    if (seconds < 59) {
        seconds.value = secs;
    } else {
        minutes.value = getminutes();
        seconds.value = getseconds();
    }
    secs--;
    setTimeout('Decrement()',1000);
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
</script>
</head>
<body>

<div id="timer">
This is only valid for the next <input id="minutes" type="text"    style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> : <input id="seconds" type="text" style="width: 60px; border:    none; background-color:none; font-size: 50px; font-weight: bold;"> 
</div>
<script>
   countdown();

试试这个

function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return ("0" + mins).substr(-2);
}
function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return ("0" + (secs-Math.round(mins *60))).substr(-2);
}

要填充零只需执行以下操作:

var renderedSecs;

renderedSecs = (secs < 10 ? "0" : "") + secs;

// now put renderedSecs into the HTML

不要对 secs 执行此操作,因为它是 number,而 renderedSecsstring

Keep the actual numbers for your computation apart from their rendering (as strings).