Skip to content

Commit

Permalink
fix(countdown): 修复倒计时结束事件多次触发的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
koppthe committed Dec 23, 2018
1 parent ce4b3db commit c7915a1
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/components/countdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,38 @@ export default class AtCountdown extends AtComponent {
}

setTimer () {
this.timer = setInterval(() => {
let [day, hours, minutes, seconds] = [0, 0, 0, 0]
if (this.seconds > 0) {
day = Math.floor(this.seconds / (60 * 60 * 24))
hours = Math.floor(this.seconds / (60 * 60)) - (day * 24)
minutes = Math.floor(this.seconds / 60) - (day * 24 * 60) - (hours * 60)
seconds = Math.floor(this.seconds) - (day * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60)
}
this.setState({ day, hours, minutes, seconds })
this.seconds--
if (this.seconds < 0) {
clearInterval(this.timer)
this.timer = null
this.props.onTimeUp()
}
}, 1000)
if (!this.timer) this.countdonwn()
}

clearTimer () {
if (this.timer) {
clearInterval(this.timer)
clearTimeout(this.timer)
this.timer = null
}
}

countdonwn () {
let [day, hours, minutes, seconds] = [0, 0, 0, 0]

if (this.seconds > 0) {
day = Math.floor(this.seconds / (60 * 60 * 24))
hours = Math.floor(this.seconds / (60 * 60)) - (day * 24)
minutes = Math.floor(this.seconds / 60) - (day * 24 * 60) - (hours * 60)
seconds = Math.floor(this.seconds) - (day * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60)
}

this.setState({ day, hours, minutes, seconds })
this.seconds--

if (this.seconds < 0) {
this.clearTimer()
this.props.onTimeUp()
return
}

this.timer = setTimeout(() => {
this.countdonwn()
}, 1000)
}

componentWillReceiveProps (nextProps) {
Expand Down

0 comments on commit c7915a1

Please sign in to comment.