React
timer
by blueseal
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
React
class TodoApp extends React.Component {
constructor(props){
super(props)
this.state = {
time: 0,
isOn: false,
start: 0
}
this.startTimer = this.startTimer.bind(this)
this.stopTimer = this.stopTimer.bind(this)
this.resetTimer = this.resetTimer.bind(this)
}
startTimer() {
this.setState({
isOn: true,
time: this.state.time,
start: Date.now() - this.state.time
})
this.timer = setInterval(() => this.setState({
time: Date.now() - this.state.start
}), 1);
}
stopTimer() {
this.setState({isOn: false})
clearInterval(this.timer)
}
resetTimer() {
this.setState({time: 0, isOn: false})
}
render() {
let start = (this.state.time == 0) ?
<button onClick={this.startTimer}>start</button> :
null
let stop = (this.state.time == 0 || !this.state.isOn) ?
null :
<button onClick={this.stopTimer}>stop</button>
let resume = (this.state.time == 0 || this.state.isOn) ?
null :
<button onClick={this.startTimer}>resume</button>
let reset = (this.state.time == 0 || this.state.isOn) ?
null :
<button onClick={this.resetTimer}>reset</button>
return (
<div>
<h3>timer: {this.state.time}</h3>
{start}
{resume}
{stop}
{reset}
</div>
)
}
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))