React native 如何阻止计时器变为负数?
我已经做了一个计时器,我想重置后,它达到0秒,但现在它只是继续向负值。我怎样才能阻止它变成负值?当计时器为0秒时,如何向快照添加+1React native 如何阻止计时器变为负数?,react-native,React Native,我已经做了一个计时器,我想重置后,它达到0秒,但现在它只是继续向负值。我怎样才能阻止它变成负值?当计时器为0秒时,如何向快照添加+1 const Timer = () => { const [seconds, setSeconds] = useState(60); const [isActive, setIsActive] = useState(false); const [shots, setShots] = useState(0); const bellSound =
const Timer = () => {
const [seconds, setSeconds] = useState(60);
const [isActive, setIsActive] = useState(false);
const [shots, setShots] = useState(0);
const bellSound = new Sound('bell.mp3');
const tickSound = new Sound('tick.mp3');
function start() {
setIsActive(true);
}
function pause() {
setIsActive(false);
}
function reset() {
setSeconds(60);
setIsActive(false);
}
function playSound() {
bellSound.play();
}
function playCountdown() {
tickSound.play();
}
function updateUI() {
}
useEffect(() => {
let interval: any = null;
if (isActive) {
interval = setInterval(() => {
setSeconds((seconds) => seconds - 1);
if (seconds === 0) {
clearInterval(interval)
return
}
}, 1000);
} else if (!isActive && seconds !== 0) {
clearInterval(interval);
} else if (shots < 100) {
clearInterval(interval)
interval = setInterval(() => {
updateUI()
}, 500)
}
return () => clearInterval(interval);
}, [isActive, seconds]);
const Timer=()=>{
常数[秒,设置秒]=使用状态(60);
常量[isActive,setIsActive]=useState(false);
常量[shots,setShots]=useState(0);
const bellSound=新声音('bell.mp3');
const tickSound=新声音('tick.mp3');
函数start(){
setIsActive(真);
}
函数暂停(){
setIsActive(假);
}
函数重置(){
设置秒(60);
setIsActive(假);
}
函数playSound(){
bellSound.play();
}
函数播放倒计时(){
滴答声。播放();
}
函数updateUI(){
}
useffect(()=>{
let interval:any=null;
如果(isActive){
间隔=设置间隔(()=>{
设置秒((秒)=>秒-1);
如果(秒===0){
清除间隔(间隔)
返回
}
}, 1000);
}如果(!isActive&&seconds!==0),则为else{
间隔时间;
}否则,如果(射程<100){
清除间隔(间隔)
间隔=设置间隔(()=>{
updateUI()
}, 500)
}
return()=>clearInterval(interval);
},[isActive,秒];