0

I have multiple html elements with the class "countdown":

<span data-seconds="95" class="countdown">00:01:35</span>
<span data-seconds="30" class="countdown">00:00:30</span>

and so on.. Now I want make a timer that countdown the value to 00:00:00, and then the page should be reload. This I have in my script:

        // Countdown
        setInterval(function(){
            $(".countdown").each(function(){
                var seconds = $(this).data('seconds');
                if(seconds > 0) {
                    second = seconds - 1;
                    $(this).data('seconds', second)
                    $(this).html(second )
                }
            });
        }, 1000);

This works perfectly for seconds ... but can somebody help me to convert this into the hh:mm:ss format? in php there is the awesome function gmdate("H:i:s", $seconds) function.. I wonder there is some similar in JS?

And - how can I trigger the page reload if $(this).data('seconds') has 0?

Thanks for reply :)

John Conde
  • 217,595
  • 99
  • 455
  • 496
goldlife
  • 1,949
  • 3
  • 29
  • 48
  • and for what reason I get downvotes? I think I am describe my problem... and format this question in a readable format. I search here for my problem, but can't figure out a solution for my problem, so I ask here in a new question. – goldlife Jan 30 '15 at 13:44

1 Answers1

3

Here is your solution,, JS BIN

    // Countdown
    setInterval(function(){
        $(".countdown").each(function(){
            var seconds = $(this).data('seconds');
            if(seconds > 0) {
                second = seconds - 1;
                $(this).data('seconds', second)

                 var date = new Date(null);
                 date.setSeconds(second); 
                 $(this).html(date.toISOString().substr(11, 8))
            }
          else
            {
              $(this).html("Reload" )
            }
        });
    }, 1000);
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
  • 1
    works perfectly.. thanks!!! only the reload was not work. But I found this: http://stackoverflow.com/questions/5404839/how-can-i-refresh-a-page-with-jquery – goldlife Jan 30 '15 at 13:40