React计时器挂起:00(状态未更新)
React timer hanging on :00 (state not updating)
React中有一个基本计时器,可以正常倒计时。唯一的问题是,当计时器的秒数达到"00"时,它会多挂一秒(或两秒),然后切换到"59"。我想我仍然对这种状态感到不舒服,因为我花了一些时间调试,似乎无法解决问题。
非常感谢您的帮助。非常感谢。
代码:(问题似乎在计时器功能中的if语句内部)
const Clock = React.createClass({
getInitialState: function() {
return { currentCount: this.props.minutes, seconds: 10 };
},
startTimer: function() {
var intervalId = setInterval(this.timer, 1000);
this.setState({ intervalId: intervalId, minutes: this.state.currentCount - 1 });
},
pauseTimer: function() {
clearInterval(this.state.intervalId);
this.setState({ intervalId: this.props.minutes });
},
timer: function() {
var minutes = this.state.minutes;
var seconds = this.state.seconds;
if (seconds === '00') {
this.setState({ minutes: '0' + minutes - 1, seconds: 60 });
this.setState({ currentCount: minutes + ':' + seconds });
console.log('min: ' + minutes, 'sec: ' + seconds);
} else {
seconds--;
seconds = seconds < 10 ? '0' + seconds : seconds;
minutes = minutes < 10 ? '0' + minutes : minutes;
this.setState({ currentCount: minutes + ':' + seconds, minutes: this.state.minutes, seconds: seconds });
}
},
componentWillReceiveProps: function(nextProps) {
this.setState({ currentCount: nextProps.minutes });
},
render: function() {
return (
<section>
<button onClick={this.startTimer}>Start</button>
<button onClick={this.pauseTimer}>Pause</button>
<br></br>
{this.state.currentCount}
</section>
);
}
});
module.exports = Clock;
试着在渲染函数中保留显示逻辑(将零左填充到分钟和秒),并用59秒而不是60秒来勾选分钟,例如:https://jsfiddle.net/reactjs/69z2wepo/
const Clock = React.createClass({
getInitialState: function() {
return { minutes: this.props.minutes, seconds: this.props.seconds };
},
startTimer: function() {
var intervalId = setInterval(this.timer, 1000);
this.setState({ intervalId: intervalId });
},
pauseTimer: function() {
clearInterval(this.state.intervalId);
this.setState({ intervalId: this.props.minutes });
},
timer: function() {
var minutes = this.state.minutes;
var seconds = this.state.seconds;
if (seconds === 0) {
this.setState({ minutes: minutes - 1, seconds: 59 });
} else {
seconds--;
this.setState({ minutes: minutes, seconds: seconds });
}
},
render: function() {
var s = this.state.seconds,
m = this.state.minutes;
return (
<section>
<button onClick={this.startTimer}>Start</button>
<button onClick={this.pauseTimer}>Pause</button>
<br></br>
{m < 10 ? '0' + m : m}:{s < 10 ? '0' + s : s}
</section>
);
}
});
相关文章:
- Web应用程序,将成员状态更新为其他成员
- Universal React/redux未在状态更新时呈现
- React.js组件中的状态更新在我的页面下留下了空白
- Ajax/Javascript用户状态更新
- 使用ReactJS使用Redux的简单状态更新事件
- Yii:TbCollapse 多链接(href = #collapse 并将状态更新为 be=“1”)
- 当 redux 状态更新时,在组件上调用 forceUpdate()
- 如何更改输入字段值,使其不会在每次更改时贯穿整个 Redux 状态更新周期
- 使用 Google 表格中列出的地址发送单件状态更新电子邮件
- 评论仅发布到最新的状态更新
- 强制 Twitter Epi 库发布状态更新,即使它是上一条推文的副本
- 使用Facebook javascript sdk获取最新的5个状态更新
- 正确处理多个连续状态更新
- 将状态更新到输入字段
- 如何在JavaScript中创建Twitter状态更新,并从中接收响应
- 如何从web url加载元数据,类似于facebook在状态更新时发布url的做法.
- Onkeyup或onkeypress javascript事件问题-密码状态更新
- WshShell运行不等待,但执行状态更新(运行vs执行问题)
- Web服务——异步执行和状态更新
- 点击后显示FB状态更新弹出窗口