I am creating a web application. Upon page loading, a routine starts, "looping" through each workout (without using keypress or clicks). I am using setInterval
to append different "workouts" to the HTML. I would like to append each workout with a countdown (that lets the user know when the workout is over and the next one starts).
(note that these if/else statements will be refactored DRY later on)
This is what I have:
$(document).ready(function() {
var counter = 470; //total length of routine in seconds
var countDown = setInterval(function(){
counter-- //decrease the counter each second
if (counter === 470) {
$(".routine").append("<p class='itemName'>Jumping Jacks!</p>");
}
else if (counter === 440) {
$(".routine").empty().append("<p class='itemName'>Take a break!</p>");
}
else if (counter === 430) {
$(".routine").empty().append("<p class='itemName'>Lounges!</p>");
}
else if (counter === 400) {
$(".routine").empty().append("<p class='itemName'>Take a Break!</p>");
}
else if (counter === 390) {
$(".routine").empty().append("<p class='itemName'>Sit Ups!</p>");
}
else if (counter === 360) {
$(".routine").empty().append("<p class='itemName'>Take break!</p>");
}
else if (counter === 350) {
$(".routine").empty().append("<p class='itemName'>Plank!</p>");
}
// will add additional workouts & breaks
else if (counter === 0) {
console.log("Finished!!");
clearInterval(countDown);
}
}, 1000);
I have tried: