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

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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