Created
November 12, 2018 01:12
-
-
Save benjaminadk/193813dcc24dd172378c0ee86b76b2af to your computer and use it in GitHub Desktop.
Simple Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Component extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
time: null | |
} | |
} | |
componentDidMount() { | |
const time = this.props.exam.time * 60 // time in minutes * 60 seconds/minute = total seconds | |
this.setState({time}) | |
} | |
initTimer = () => { | |
this.timer = setInterval(() => { | |
const {time} = this.state | |
if (time === 0) { | |
clearInterval(this.timer) | |
// end the exam | |
} | |
this.setState({ time: time - 1 }) // decrease time by one second every second | |
}, 1000) | |
} | |
pauseTimer = () => { | |
clearInterval(this.timer) | |
// show some ui to indicate timer is paused | |
} | |
render() { | |
return (...) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment