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

135
Visualizações
Toggle button using a flag React

When a button is pressed, it calls the toggleTimer() function. Now, when I press this button, it pauses immediately, however when I press the button a second time, it does not unpause immediately. The goal is to get the button to toggle between these 2 states immediately after being pressed.

When I run the program I notice weirdly that I have to press my button many times before I get the unpause behaviour to work. I think this is due to the let paused=true global variable, but I'm unsure how to solve this.

///////PAUSE TIMER/////////////////
  let paused = true;
  let holdValue;

  function toggleTimer(){
    if (paused===true){ // if paused, we clear timer and save value
      paused = false; 
      holdValue= remainingTime;
      clearInterval(timerRef.current);
    } else if (paused===false) { //if not paused, we can restart timer using our saved value
      paused = true; 
      startTimer(); // call our function to start timer
      setRemainingTime(holdValue); // update the remaining time using our saved value
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe you are right about the global var making the behaviour weird.

So solution is to use a state in your button component:

const [paused, setPaused] = useState(false)

Then you should combine it with a useEffect that will handle the timer (I'm not sure I understood correctly but you should be able to adapt it to your need):

const Example = () => {
  const [paused, setPaused] = useState(false);

  useEffect(() => {
    let timer;
    if (!paused) {
      timer = setInterval(() => {
        // Do something
      }, 10000);
    } else {
      clearInterval(timer);
    }

    return () => clearInterval(timer);
  }, [paused]);

  return <Button onClick={() => setPaused(!paused)}>Button</Button>;
};

Here when paused is false, the timer (interval) will call execute its callback (// Do something) every 10s, then when you click on the button paused change to false so the useEffect is executed and the time it clear the timer. If you want to start it again, then click and the button, that will change the paused state and the useEffect will restart the interval.

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