1.2 KiB
1.2 KiB
Ticker
Renders a ticker component.
- The ticker state is initially set to zero
- When the
Tick!button is clicked,timeris incremented periodically at the giveninterval - When the
Resetbutton is clicked, the value of the timer is set to zero and thesetIntervalis cleared - The
setIntervalis cleared once the desiredtimeis reached timeandintervalare the required props
class Ticker extends Component {
constructor(props) {
super(props);
this.state = {ticker: 0}
this.interval = null
}
tick = () => {
this.reset()
this.interval = setInterval(() => {
if (this.state.ticker < this.props.times) {
this.setState(({ ticker }) => ({ticker: ticker + 1}))
}else{
clearInterval(this.interval)
}
}, this.props.interval)
}
reset = () => {
this.setState({ticker: 0})
clearInterval(this.interval)
}
render() {
return (
<div>
<span style={{fontSize: 100}}>{this.state.ticker}</span>
<button onClick={this.tick}>Tick!</button>
<button onClick={this.reset}>Reset</button>
</div>
);
}
}
ReactDOM.render(<Ticker times={5} interval={1000} />, document.getElementById('root'));