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

208
Vistas
Restart timer component with new duration after onComplete event in React

Using react-countdown-circle-timer to render a countdown timer. Trying to create an event handler that changes the duration of the timer each time the timer reaches zero.

The clock initially runs down from 5 seconds, when it reaches zero I change the state, then use this to change the new value assigned to the clock.

For a more practical example. When the clock initially runs to zero, I change my stage state to 2, setting the timer to 6 seconds. When this clocks runs down, I change my stage state to 3, setting the timer to 7 seconds and so on, until I stop at stage 5

This is my code:

  const [ timer, setTimer ] = useState(5)
  const [stage, setStage] = useState(1)
  const timerMap = {
        2: 6,
        3: 7,
        4: 8,
        5: 9
    }
  const changeTimeHandler = (timer, setTimer) => {
        setStage(stage+1)
        setTimer(timerMap[stage])
        return { delay:1 }
  }
    console.log(timer)
  return (
    <div className="App">
      <div className="timer-wrapper">
        <CountdownCircleTimer
          isPlaying
          duration={timer}
          colors={["#004777", "#F7B801", "#A30000", "#A30000"]}
          colorsTime={[10, 6, 3, 0]}
          onComplete={() => (changeTimeHandler(timer, setTimer))}
          size={100}
        >
          {renderTime}
        </CountdownCircleTimer>
      </div>
    </div>
  );

When I run my code I get the error:

Warning: Received NaN for the 'strokeDashoffset' attribute. If this is expected, cast the value to a string.

Need help figuring out how to build the logic to get the desired effect.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You don't have any condition to stop the iteration.

A simple solution would probably be:

const changeTimeHandler = (timer, setTimer) => {

  // -- stop if no more stages available
  if( !timerMap[stage] ){
    return { shouldRepeat: false } // <-- Note: Maybe not 100% correct. I don't know this libraray.
  }

  setStage(stage+1)
  setTimer(timerMap[stage])
  return { delay:1 }
}
about 4 years ago · Santiago Trujillo Denunciar
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