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

174
Visualizações
How a run count down again after 3 seconds in Javascript?

The circular count down from 10 to 1 is working properly but I want to start the count down again (from 10 to 1 without reload) after a pause of 3 seconds. I have tried using a setTimeOut within a setTimeOut but it is not working. Here is the code.

var countdownNumberEl = document.getElementById('countdown-number');
var countdown = 10;

countdownNumberEl.textContent = countdown;

setInterval(function() {
  countdown = --countdown <= 0 ? 10 : countdown;

  countdownNumberEl.textContent = countdown;
  
  setInterval(function(){
 countdownNumberEl.textContent = countdown;

  },3000)
}, 1000);
<div id="countdown">
  <div id="countdown-number"></div>
  <svg>
    <circle r="18" cx="20" cy="20"></circle>
  </svg>
</div>

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

just add 3 sec as initial and change textContent when it will be less then 10

let countdownNumberEl = document.getElementById('countdown-number');
let countdown = 10;

countdownNumberEl.textContent = countdown;

setInterval(function() {
  countdown = --countdown <= 0 ? 13 : countdown;

  if (countdown > 10) {
     return;
  }
  countdownNumberEl.textContent = countdown;
}, 1000);
<div id="countdown">
  <div id="countdown-number"></div>
  <svg>
    <circle r="18" cx="20" cy="20"></circle>
  </svg>
</div>

about 4 years ago · Santiago Trujillo Relatório

0

I tried it with a pointer function. I stop the interval at reaching 1, and use setTimeout() to start a new interval.

startInterval = () => {
  // setup markup and count
  let countdownNumberEl = document.getElementById('countdown-number');
  let countdown = 10;
  countdownNumberEl.textContent = countdown;

  // start the interval
    intervalId = setInterval(function () {
    --countdown;
    if(countdown < 1) {
      // stop interval
      clearInterval(intervalId); 
      // pause and start a new interval
      setTimeout(startInterval, 3000);         
    } else {
      // update markup
      countdownNumberEl.textContent = countdown;
    }
  }, 1000);
}

startInterval();
<div id="countdown">
  <div id="countdown-number"></div>
  <svg>
    <circle r="18" cx="20" cy="20"></circle>
  </svg>
</div>

ref: Javascript setInterval function to clear itself?

about 4 years ago · Santiago Trujillo Relatório

0

Since you mentioned that you have tried to solve the problem with nested setTimeout functions. It is also possible to do that:

const countdownNumberEl = document.getElementById('countdown-number');

const repeatCountdownFrom = (n, waitTimeout) => {
  countdownFrom(n, () => 
    setTimeout(() => 
      repeatCountdownFrom(n, waitTimeout)
    , waitTimeout)
  )
}

const countdownFrom = (n, callbackWhenDone) => {
  if(n >= 0) {
    setTimeout(() => {
      countdownNumberEl.textContent = n;
      countdownFrom(n - 1, callbackWhenDone);
     }, 1000);
  } else {
    callbackWhenDone();
  }
}

repeatCountdownFrom(10, 3000);
<div id="countdown-number"></div>

about 4 years ago · Santiago Trujillo 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