Skip to content

Commit

Permalink
fix(count-down): 回收定时器
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczj committed Nov 5, 2018
1 parent f0e09cb commit 5c01fed
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/count-down/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ export default class AtCountDown extends AtComponent {
const { day, hours, minutes, seconds } = this.props
this.seconds = (day * 60 * 60 * 24) + (hours * 60 * 60) + (minutes * 60) + seconds
this.state = { day, hours, minutes, seconds }
this.timer = null
}

formatNum (num) {
return num <= 9 ? `0${num}` : `${num}`
}

componentDidMount () {
const timer = setInterval(() => {
this.timer = setInterval(() => {
let [day, hours, minutes, seconds] = [0, 0, 0, 0]
if (this.seconds > 0) {
day = Math.floor(this.seconds / (60 * 60 * 24))
Expand All @@ -67,12 +68,19 @@ export default class AtCountDown extends AtComponent {
this.setState({ day, hours, minutes, seconds })
this.seconds--
if (this.seconds < 0) {
clearInterval(timer)
clearInterval(this.timer)
this.timer = null
this.props.onTimeUp()
}
}, 1000)
}

componentWillUnmount () {
if (this.timer) {
clearInterval(this.timer)
}
}

render () {
const {
className,
Expand Down

0 comments on commit 5c01fed

Please sign in to comment.