【发布时间】:2021-05-31 21:47:10
【问题描述】:
我正在构建一个在一秒钟内计算点击次数的 Web 应用程序,但是 倒计时(从 5 到 0)不起作用。倒计时应该每秒(1000 毫秒)减少一次。我已经尝试使用 setInterval() 但它不起作用,现在我不知道我必须做什么
import React, {Component} from 'react';
import './App.scss';
class App extends Component {
state = {
count: 0,
cps: 0,
seconds: 5,
target: '',
}
five = 5000;
fiveSeconds = () => {
this.increment = () => {
this.setState({
count: this.state.count + 1,
})
}
this.countdown = setInterval(() => {
this.setState({ seconds: this.state.seconds - 1})
if (this.state.seconds <= 0){
this.setState({ seconds: 0 });
}
}, 1000)
this.timeout = setTimeout(() => {
this.setState({ target: 'true', cps: this.state.count/this.five*1000 });
if(this.state.count == 59){
this.setState({ cps: 11.8 })
}
}, this.five)
}
render(){
return (
<div>
<h3 id="counter-title">COUNTER: <a href="/" id="counter">{this.state.count}</a></h3>
<h3 id="counter-title">CPS: {this.state.cps}</h3>
<h3>TIMER: {this.state.seconds}</h3>
<br/>
<button disabled={this.state.target} id="btn" onClick={this.fiveSeconds}>CLICK HERE TO START</button>
</div>
);
}
}
export default App;
【问题讨论】:
标签: javascript reactjs timer countdown