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

242
Visualizações
React Native setInterval Works Slowly

I am trying to create stopwatch application. I'm using setInterval function to increase time. But my app to stay behind windows clock app.

windows clock app: windows clock app

my app: my app

I launched both at same time.

App.js

export default function App() {
const [time, setTime] = useState({ running: false, time: 0 });
const [count, setCount] = useState(time.time);

useEffect(() => {
    console.log(count);
    let id;
    if (time.running)
      id = setInterval(() => setCount(count => count + 100), 100);

    return () => { 
      if (time.running) 
        clearInterval(id);
    };
  }, [time.running]);

  const counter = () => {
    if (time.running) {
      setTime({
        running: false,
        time: time.time
      });
    } else {
      setTime({
        running: true,
        time: time.time
      });
    }
  };
}


return (
    <TimeContext.Provider value={{ time, setTime }}>
      <SafeAreaView style={[styles.container, { backgroundColor: theme.background }]}>
        <View style={styles.stopwatch}>
          <Stopwatch time={count} />
        </View>
      </SafeAreaView>
    </TimeContext.Provider>
  );

Stopwatch.js

export default function Stopwatch({ time }) {
  return (
    <Text style={[styles.text, { color: theme.text }]}>{msToTime(time)}</Text>
  );
}

ms-to-time.js

export default function msToTime(duration) {
  var milliseconds = Math.floor((duration % 1000) / 100),
    seconds = Math.floor((duration / 1000) % 60),
    minutes = Math.floor((duration / (1000 * 60)) % 60),
    hours = Math.floor((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}

When click start button counter function triggers. I'm increasing count variable instead of time.time cuz other components using time so i blocking render others this way.

about 4 years ago · Juan Pablo Isaza
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