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.
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 🙂