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

121
Vistas
Countdown Timer using functional component in ReactJS doesn't work properly

I want to make a countdown timer with start, stop, resume and reset buttons. However, I could not figure out why my code does not work. I am guessing that the issue lies in the part on timer, setInterval and clearInterval.

I have attached snippets of my code below.

Help would be greatly appreciated, thanks!

function Countdown() {

    const [ timerRunning, setTimerRunning ] = useState(false);
    const [ startTime, setStartTime ] = useState(0);
    const [ totalTime, setTotalTime ] = useState(0);

    var timer;

    const startTimer = () => {
        setTimerRunning(true);
        setStartTime(totalTime);
        setTotalTime(totalTime);
        
        timer = setInterval(() => {
            const remainingTime = totalTime - 1000;
            if (remainingTime >= 0) {
                setTotalTime(remainingTime);
            } else {
                clearInterval(timer);
                setTimerRunning(false);
                setStartTime(0);
                setTotalTime(0);
            }
        }, 1000);
    };

    const stopTimer = () => {
        clearInterval(timer);
        setTimerRunning(false);
    };
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you have to put your setInterval in React.useEffect take a look at this post some suggestions for your code:

  • use className instead of class attribute in your elements.
  • don't use var or let in react functional components.
about 4 years ago · Juan Pablo Isaza Denunciar

0

useState doesn't change value immediately. So it's value is not actually changing. To avoid this use UseEffect hook.

useEffect(() => {
        let intervalid
      if (timerRunning){
          intervalid = setInterval(() => {
            const remainingTime = totalTime - 1000;
            if (remainingTime >= 0) {
                
                setTotalTime(remainingTime);
            } else {
                clearInterval(intervalid);
                setTimerRunning(false);
                setStartTime(0);
                setTotalTime(0);
            }
          }, 1000);
      }

    
      return () => {
        clearInterval(intervalid)
      }
    }, [timerRunning,totalTime]) 
about 4 years ago · Juan Pablo Isaza 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