Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda