0
votes

I am trying to create a timer that is displayed on screen that starts at 90 minutes and submits the page when the time goes to 0. Right now I can get it to work, but I can't display the minutes, only seconds (90 min = 5400 sec). I would like for the time to say something like 20 minutes and 5 seconds remaining instead of 1205 seconds remaining.

CODE:

<p>Time remaining: <span id="count">5400</span> seconds</p>

<script type="text/javascript">

window.onload = function(){

(function(){
  var counter = 5400;

  setInterval(function() {
    counter--;
    if (counter >= 0) {
  span = document.getElementById("count");
  span.innerHTML = counter;
}

if (counter === 0) {
    //i will write the page submit code here

}

}, 1000);

})();

}


</script>
1

1 Answers

0
votes

Heres a complete vanilla JS countdown I wrote. It includes days as well, if you leave days and hours zero, they simply wont be displayed so you can use it if you want:

Note the whole thing runs in an interval function which gets reCalled every second.

//Countdown
if(document.getElementById('s') != null)
{
    s = parseInt(document.getElementById('s').textContent);
    m = parseInt(document.getElementById('m').textContent);
    h = parseInt(document.getElementById('h').textContent);
    d = parseInt(document.getElementById('d').textContent);
    var countdownField = document.getElementById('countDown');

setInterval(function(){

    countdownField.textContent ="Verbleibend: "+d+" Tage "+h+" Stunden "+m+" Minuten "+s+" Sekunden ";

    if(s==0 && m==0 && h==0 && d==0) 
        location.reload(); 
    else if(m==0 && h==0 && d==0)
        countdownField.textContent ="Verbleibend: "+s+" Sekunden ";
    else if(h==0 && d==0)
        countdownField.textContent ="Verbleibend: "+m+" Minuten "+s+" Sekunden ";
    else if(d==0)
        countdownField.textContent ="Verbleibend: "+h+" Stunden "+m+" Minuten "+s+" Sekunden ";

        if(s == 0){
            s = 60;
            m--;
         }
         if(m == 0 && s == 0){
            m = 59;
            h--;
          }
          if(h == 0 && m == 0 && s == 0){
             h = 23;
             d--;
          }
        s--;
}, 1000);
}

Edit Some help with understanding

Verbleibend == Remaining

Tage == Days

Stunden == Hours

Minuten == Minutes

Sekunden == Seconds