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
相关产品推荐
相关产品推荐

