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

83
Visualizações
Create a react state that changes with time

I am creating a game with react and i want to add a chronometre feature to track time but i tried to use settimeout and it failed bcs every render the settimeout recall and i tried setinterval but failed also can anyone suggest me a way to to this the game is a tenzies game

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you are using a functional component then you are going to have to set an interval constant in the useEffect() hook:

useEffect(() => {
  const interval = setInterval(() => {
    //code inside here will run every second
  }, 1000); //change the 1000 to however many miliseconds you want between execution
  return () => clearInterval(interval);
}, []);

This code will run whatever is in the setInterval body every second, but this amount can be changed (change the '1000'). The interval will then be cleared. Note that this interval will stop counting when the component is unmounted

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