开发者

JavaScript countdown timer not starting

开发者 https://www.devze.com 2023-03-22 10:41 出处:网络
I tried using this JavaScript countdown timer on my page but the timer won\'t start. What am I doing wrongly?

I tried using this JavaScript countdown timer on my page but the timer won't start. What am I doing wrongly?

var CountdownID = null;
var start_msec开发者_运维问答ond = 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>


here is what you need to do first

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

should be

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

also you should avoid using a string in your setTimeout function:

CountdownID = window.setTimeout(function () {
   countDown(pmsecond,psecond,"timerID");
}, 100);


See here http://jsbin.com/ifiyad/2/edit

0

精彩评论

暂无评论...
验证码 换一张
取 消