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

118
Visualizações
React Countdown Clock not sure if issue is with my setInterval or my setState

I've been working on FreeCodeCamp Front End development project 25 + 5 Clock. I'm trying to click the play button and make it countdown. I know it enters my countdown function but when logging it, I think it is only calling once. So I think my setInterval might be wrong but I've also struggled with using setState on all projects. They have only taught using this.setState and this has often caused problems because the state is not what I expect it to be (like it's not immediate). I'm not ready to dive into the concepts of DidMount, WillMount, etc so I'd like to stay in the boundaries of using setState if that is where my problem is. Below is a link to my codepen

25 + 5 Clock Codepen

handleCountDown(pause){
let timer;
if(pause){
  clearInterveral(timer)
} else {
  if(this.state.minutesLeft === '' && this.state.secondsLeft === ''){
    if(this.state.session){
        timer =  setInterval(this.countdown(Number(this.state.sessionLength), 0), 1000)
    } else {
      timer =  setInterval(this.countdown(Number(this.state.breakLength), 0), 1000)
    }
  } else {
    timer =  setInterval(this.countdown(Number(this.state.minutesLeft), Number(this.state.secondsLeft)), 1000)
  }   
}}

countdown(min, sec){
const second = 1000;
const minutes = second * 60;

const countDate = (new Date().getTime()) + (second * sec) + (minutes * min);//target time
const now = new Date().getTime();
const gap = countDate - now;



const textMinute = Math.floor(gap / minutes);
const textSecond = Math.floor((gap % minutes)/ second);

if(textSecond === 0){
  textSecond = '00'
}
this.setState({
  minutesLeft:textMinute.toString(),
  secondsLeft:textSecond.toString()
})}
about 4 years ago · Juan Pablo Isaza
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