0

I need to convert via jquery, on page load, a text time shown as a <span class="countdown-timer">Nov-07-2021 08:34:00 EST</span> into a timer. On same page there are various times, not just that specific timer so the script should run all the classes .countdown-timer and show the proper countdown, based on the time that each have.

The code that i am using works just for minutes and also it seems to start over instead of stopping or showing ideal, a ENDED message instead of the countdown.

var interval = setInterval(function() {
    var timer = $('span').html();
    timer = timer.split(':');
    var minutes = parseInt(timer[0], 10);
    var seconds = parseInt(timer[1], 10);
    seconds -= 1;
    if (minutes < 0) return clearInterval(interval);
    if (minutes < 10 && minutes.length != 2) minutes = '0' + minutes;
    if (seconds < 0 && minutes != 0) {
        minutes -= 1;
        seconds = 59;
    }
    else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
    $('span').html(minutes + ':' + seconds);
    
    if (minutes == 0 && seconds == 0)
        clearInterval(interval);
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span>Nov-07-2021 08:34:00 EST</span>
Adyyda
  • 335
  • 3
  • 16
  • Does this answer your question? [Parsing a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/parsing-a-string-to-a-date-in-javascript) – Heretic Monkey Nov 05 '21 at 11:43
  • The real question is WHY doing that ? Seem so weird logic... – bZezzz Nov 05 '21 at 11:44
  • I try to display a counter like 13h, 36m, 15s instead of Nov-07-2021 08:34:00 EST. Why is this weird? – Adyyda Nov 05 '21 at 11:48

0 Answers0