I have a timer that counts down to 0. It works fine with one, but I want to create a second timer on the same page. I can create the second timer, but it won't countdown and the first one counts down twice as fast. How would I do this. Here is my code
<script>
var Timer;
var TotalSeconds;
function CreateTimer(TimerID, Time) {
Timer = document.getElementById(TimerID);
TotalSeconds = Time;
UpdateTimer()
window.setTimeout("Tick()", 1000);
}
function Tick() {
if(TotalSeconds <= 0) {
TotalSeconds = 0;
return;
}
TotalSeconds -= 1;
UpdateTimer()
window.setTimeout("Tick()", 1000);
}
function UpdateTimer() {
var Seconds = TotalSeconds;
var Days = Math.floor(Seconds / 86400);
Seconds -= Days * 86400;
var Hours = Math.floor(Seconds / 3600);
Seconds -= Hours * (3600);
var Minutes = Math.floor(Seconds / 60);
Seconds -= Minutes * (60);
var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)
Timer.innerHTML = TimeStr;
}
function LeadingZero(Time) {
return(Time < 10) ? "0" + Time : +Time;
}
</script>
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$currentTime = time();
$today = $tomorrow - $currentTime;
?>
<div id="timer"></div>
<script type="text/javascript">
var today = <?php echo $today; ?>
window.onload = CreateTimer("timer", today);
</script>
<div id="timer2"></div>
<script type="text/javascript">
var today = <?php echo $today; ?>
window.onload = CreateTimer("timer2", today);
</script>
.setTimeout()should be passed a function handler, not a string "Tick()". You don't want to make setTimeout run that witheval(), easier to just pass it the handler. If you havevar tick = function() {....};Then you can do this:window.setTimeout(tick, 1000);TimerandTotalSecondsvariables in a closure so they are private to each timer. Incidentally, it's a convention that variable names starting with a capital letter are reserved for constructors.