Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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)

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);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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')

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda