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

323
Visualizações
setInterval only executes once

I'm working on a React component where on form submit, a timer takes in the value input from a slider component and starts to count down to zero. However, it is only the timer stops after 1 of the setInterval function in triggerClock(). I'm not sure what to do.

My React component:

const Home = (props) => {
  let timeInMinutes;
  let timeInSeconds;

  const [time, setTime] = useState(0);
  const [minutes, setMinutes] = useState(0);
  const [seconds, setSeconds] = useState(0);

  const handleTimeChange = (event) => {
    setTime(event.target.value);
  };

  const triggerClock = () => {
    const interval = setInterval(() => {
      if (minutes === 0 && seconds === 0) {
        clearInterval(interval);
      }

      if (seconds === 0) {
        setSeconds((sec) => sec + 59);
        setMinutes((min) => min - 1);
      } else {
        setSeconds((sec) => sec - 1);
      }
    }, 1000);

    return () => {
      clearInterval(interval);
    };
  };

  const handleFormSubmit = (event) => {
    event.preventDefault();
    timeInMinutes = time;
    timeInSeconds = timeInMinutes * 60;
    setMinutes(timeInSeconds / 60);
    setSeconds(timeInSeconds % 60);
    triggerClock();
  };

  return (
    <React.Fragment>
      <Header />
      <Container maxWidth="xs" sx={{ textAlign: "center" }}>
        <Typography variant="h5">Set Time (Minutes)</Typography>
        <Box component="form" onSubmit={handleFormSubmit}>
          <Slider
            aria-label="Timer"
            valueLabelDisplay="auto"
            step={10}
            marks
            min={0}
            max={60}
            value={time}
            onChange={handleTimeChange}
          />
          <Button type="submit">Submit</Button>
        </Box>
        <div>
          {minutes}:{seconds}
        </div>
      </Container>
    </React.Fragment>
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are a few issues here; first I'd suggest you have a read of Dan Abramov's excellent Making setInterval declarative blog post which is a great way to get your head around the tricky intersection of setInterval and React Hooks.

Firstly, you're putting too much stuff into React state. Your minutes and seconds can be directly, and trivially, derived from time. We don't need React to look after them for us, they are relevant only during "this render". so:

  const [time, setTime] = useState(0);

  const minutes = Math.floor(time / 60);
  const seconds = time % 60;

Secondly, in the spirit of Dan's quote:

"we can describe the process at all points in time simultaneously"

we need to know, and control, whether the timer is counting down or not:

const [timerRunning, setTimerRunning] = useState(false);

...

const triggerClock = () => {
  setTimerRunning(true);
};

Note that an additional benefit of this is we can now make our GUI reflect the state:

<Button disabled={timerRunning} type="submit">Submit</Button>

And finally, we have all the necessary parts to write a useEffect that will encompass the setInterval with the desired countdown, stop at the right time and clean up the interval:

  useEffect(() => {
    if (timerRunning) {
      const interval = setInterval(() => {
        setTime((oldTime) => {
          const newTime = oldTime - 1;
          if (newTime === 0) {
            setTimerRunning(false);
          }
          return newTime;
        });
      }, 1000);
      return () => {
        clearInterval(interval);
      };
    }
  }, [timerRunning]);
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