Answers for "onclick start and stop the count react"

0

onclick start and stop the count react

class Controls extends React.Component{
    state={
        myInterval : null,
        time: 25*60*1000
    }
    countDown = () => {
        this.setState({
            time : this.state.time-1000,
        })
    }
    startTimer = () => {
        this.setState({myInterval : setInterval(this.countDown,1000)})
    }
    stopTimer = () => {
        clearInterval(this.state.myInterval)
    }
    render(){
        return(
            <div>
                <button onClick={this.startTimer}>Start</button>
                <button onClick={this.stopTimer}>Stop</button>
                {this.state.time}
            </div>
        )
    }
}


class App extends React.Component{
    render(){
        return <Controls/>
    }
}





ReactDOM.render(
  <App />,
  document.getElementById("react")
);
Posted by: Guest on January-06-2021

Code answers related to "onclick start and stop the count react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language