Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@benjaminadk
Created November 12, 2018 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminadk/193813dcc24dd172378c0ee86b76b2af to your computer and use it in GitHub Desktop.
Save benjaminadk/193813dcc24dd172378c0ee86b76b2af to your computer and use it in GitHub Desktop.
Simple Timer
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