You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native秒表实现在iOS上运行缓慢的问题求助

React Native秒表实现在iOS上运行缓慢的问题求助

各位好~我自己实现了一个React Native的秒表组件,代码如下:

import { useState, useEffect } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';

function TimerComponent() {
  const [isActive, setIsActive] = useState(false);
  const [time, setTime] = useState(0);

  useEffect(() => {
    let interval = null;

    if (isActive) {
      interval = setInterval(() => {
        setTime(time => time + 0.1);
      }, 100);
    } else {
      clearInterval(interval);
    }
    return () => {
      clearInterval(interval);
    };
  }, [isActive]);

  const handleStart = () => {
    setIsActive(true);
  };

  const handleReset = () => {
    setIsActive(false);
    setTime(0);
  };

  const roundedTime = time.toFixed(1);

  return (
    <View style={styles.outerContainer}>
      <Button
        title="Start"
        onPress={handleStart}
      />
      <Text style={styles.text}>{roundedTime}</Text>
      <Button
        title="Stop"
        onPress={handleReset}
      />
    </View>
  );
}

export default TimerComponent;

这个组件在Android上表现完全符合预期,我拿它和其他秒表对比,两者同时走到30秒,时间完全同步。但在iOS上测试的时候就出问题了,它要比正常时间慢大概5秒左右,有没有朋友能帮忙分析下原因,或者给点解决思路呀?

备注:内容来源于stack exchange,提问作者algorhythm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:53:02