Reactjs 如何防止React Native中延迟的负值?
我想在倒计时结束后停止倒计时,现在倒计时将以负数延迟Reactjs 如何防止React Native中延迟的负值?,reactjs,react-native,Reactjs,React Native,我想在倒计时结束后停止倒计时,现在倒计时将以负数延迟 constructor(props) { super(props); this.state = { timer: 5 };} constructor(props) { super(props); this.state = { timer: 30 }; } startTimer = () => { this.clockCall = setInterval(() => { this.decrementClock(); }
constructor(props) {
super(props);
this.state = {
timer: 5
};}
constructor(props) {
super(props);
this.state = { timer: 30 };
}
startTimer = () => {
this.clockCall = setInterval(() => {
this.decrementClock();
}, 1000);
}
decrementClock = () => {
this.setState((prevstate) => ({ timer: prevstate.timer-1 }));
};
componentWillUnmount() {
clearInterval(this.clockCall);
}
有第二个参数作为回调,可以使用它检查倒计时是否已完成
decrementClock = () => {
this.setState((prevstate) => ({ timer: prevstate.timer-1 }), () => {
if (this.state.timer < 1) {
clearInterval(this.clockCall);
}
});
};
如果间隔为负值,则应清除间隔: startTimer==>{ this.clockCall=setInterval=>{ 如果this.state.timer==0{ clearIntervalthis.clockCall 回来 } 这个。递减锁; }, 1000; }
我怎样才能暂停并恢复倒计时?谢谢这是实现时钟的错误方法。要正确实施时钟,您必须在开始新日期时节省时间,并更频繁地更新,例如每100毫秒更新一次。更新时,您应该将时间设置为当前时间和开始日期的差值。对不起,我的反应技能是基本的