0
votes

I have been struggling over this countdown timer for some time. I have made a lot of progress but for some reason when minutes and seconds are 0 it isn't reducing the hours. Why is this?

code:

<?php
$timeTo  = strtotime(gmdate("12:24:00")).'<br />';
$timeNow = strtotime('now').'<br />';
$differenceInSeconds = $timeTo - $timeNow;
?>
<script type="text/javascript">
var s= "<?php Print($differenceInSeconds);?>";
var h= Math.floor(s/3600);
s-= h*3600;
var m= Math.floor(s/60);
s -= m*60;
var counter=setInterval(timer, 1000); //1000 will  run it every 1 second
    function timer()
    {

    s=s-1;
        if(s == 0 ){
        m=m-1;
        s=59;
          if(s == 0 && m == 0){
          h=h-1;
          s=59;
          m=59;
           if(s == 0 && m == 0 && h == 0){
           // reset the counter
           //return;
           }
          }
        }
//Do code for showing the number of seconds here
    document.getElementById("timer").innerHTML=h+'hrs '+m+'min '+s+'secs ';//(h <= 0 ? ' ' : h+"hr ")+(m <= 0 ? ' ' : m+"min ")+(s < 10 ? '0'+s+'secs ' : s+"secs "); // watch for spelling
                }
        </script>

<span id="timer"></span>

Heres a JSFiddle

1

1 Answers

1
votes

First check if s == 0 and then decrement it, not in opposite order:

s=s-1; // s will be -1
if(s == 0 ){

But

if (s == 0) {
    // logic here
} else {
    s = s - 1;
}

Demo: http://jsfiddle.net/VsP87/3/