Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create Time Countdown in Javascript

Iam creating a workout countdown timer using javascript.

This is my code

var counter = 30;

setInterval( function(){
  counter--;

  if( counter >= 0 ){
    id = document.getElementById("count");
    id.innerHTML = counter;
  }

  if( counter === 0 ){
    id.innerHTML = "REST";
  }
}, 1000);
<div class="container">
      <h1 id="count">WORK</h1>
</div>
    

The time countdown running well, but i want to add a rest time countdown after 30sec workout.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please Help me Thanks…

>Solution :

The following code should work. Once the workout counter reaches 0, we can switch to rest mode and rest for 30 seconds (you can change how long the user rests for as well). After these 30 seconds are up, the timer will switch back into workout mode and the loop continues.

var counter = 30;
var mode = "Workout";

setInterval( function(){
  counter--;

  if(mode == "Workout" && counter >= 0 ){
    id = document.getElementById("count");
    id.innerHTML = "Workout: " + counter;
  }
  else{
    id = document.getElementById("count");
    id.innerHTML = "Rest: " + counter;
  }

  if( counter === 0 ){
    if(mode == "Workout"){
       mode = "Rest";
    }
    else{
       mode = "Workout";
    }
    id.innerHTML = "REST";
    counter = 30;
  }
}, 1000);
<div class="container">
      <h1 id="count">WORK</h1>
</div>
    

I hope this helped! Let me know if you need any further details or clarification 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading