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

How to change class and recall the class back using javascript

Iam trying to give 2 different colors on my timer countdown apps..

on workout timer will be green.

on rest timer will be red. (and the color will be red until end)

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

my question is how to make the timer back to green after rest timer(red) and back to red after green(workout timer)

document.getElementById('start').addEventListener('click', startCountdown);

    function startCountdown() {
      let counter = 3,
        mode = "Workout",
        rounds = 1;
      const interval = setInterval(function() {
        counter--;

        if (mode == "Workout" && counter >= 0) {
          id = document.getElementById("count");
          id.innerHTML = "ROUND " + rounds + " <br> " + counter;
          id.classList.add('green') <<<THE FRIST COLOR RUNNING WELL
        } else {
          id = document.getElementById("count");
          id.innerHTML = "REST " + " <br> " + counter;
          id.classList.add('red') <<< SECOND COLOR RUNNING WELL
        }

        if (counter === 0) {
          if (mode == "Workout") {
            mode = "Rest";
          } else {
            mode = "Workout";
            rounds++;
          }
          id.innerHTML = "DONE";
          counter = 3;
          if (rounds == 21) {
            clearInterval(interval);
          }
        }

      }, 1000);

>Solution :

add this near line 9
(mode == "Workout" && counter >= 0) ? clsnm="green" : clsnm="red";
and pass this variable near id.classList.add(clsnm)

alternatively you can also try to add id.classList.remove(‘class_name’) before id.classList.add(‘class_name’)

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