我尝试在页面上使用此JavaScript倒数计时器,但计时器无法启动。
我做错了什么?

var CountdownID = null;
var start_msecond = 9;
var start_sec = 120;

window.onload = countDown(start_msecond, start_sec, "timerID");

function countDown(pmsecond, psecond, timerID) {
var msecond = ((pmsecond < 1) ? "" : "") + pmsecond;
var second = ((psecond < 9) ? "0": "") + psecond;

document.getElementById(timerID).innerHTML = second + "." + msecond;

if (pmsecond == 0 && (psecond-1) < 0) { //Recurse timer
    clearTimeout(CountdownID);

    var command = "countDown("+start_msecond+", "+start_sec+", '"+timerID+"')";
    CountdownID = window.setTimeout(command, 100);

    alert("Time is Up! Enter your PIN now to subscribe!");
}
else { //Decrease time by one second
    --pmsecond;
    if (pmsecond == 0) {
        pmsecond=start_msecond;
           --psecond;
    }
    if (psecond == 0) {
        psecond=start_sec;
    }
    var command = "countDown("+pmsecond+", "+psecond+", '"+timerID+"')";
    CountdownID = window.setTimeout(command, 100);
}
}

<span style="color:red" name="timerID" id="timerID">91.6</span>

最佳答案

见这里http://jsbin.com/ifiyad/2/edit

07-26 06:45