|
|
@ -10,6 +10,11 @@ const REFRESH_INTERVAL = 1000 / 30; |
|
|
|
interface CountdownProps extends StatisticProps { |
|
|
|
value?: countdownValueType; |
|
|
|
format?: string; |
|
|
|
onFinish?: () => void; |
|
|
|
} |
|
|
|
|
|
|
|
function getTime(value?: countdownValueType) { |
|
|
|
return interopDefault(moment)(value).valueOf(); |
|
|
|
} |
|
|
|
|
|
|
|
class Countdown extends React.Component<CountdownProps, {}> { |
|
|
@ -34,7 +39,7 @@ class Countdown extends React.Component<CountdownProps, {}> { |
|
|
|
syncTimer = () => { |
|
|
|
const { value } = this.props; |
|
|
|
|
|
|
|
const timestamp = interopDefault(moment)(value).valueOf(); |
|
|
|
const timestamp = getTime(value); |
|
|
|
if (timestamp >= Date.now()) { |
|
|
|
this.startTimer(); |
|
|
|
} else { |
|
|
@ -43,7 +48,7 @@ class Countdown extends React.Component<CountdownProps, {}> { |
|
|
|
}; |
|
|
|
|
|
|
|
startTimer = () => { |
|
|
|
if (this.countdownId !== undefined) return; |
|
|
|
if (this.countdownId) return; |
|
|
|
|
|
|
|
this.countdownId = window.setInterval(() => { |
|
|
|
this.forceUpdate(); |
|
|
@ -51,8 +56,16 @@ class Countdown extends React.Component<CountdownProps, {}> { |
|
|
|
}; |
|
|
|
|
|
|
|
stopTimer = () => { |
|
|
|
const { onFinish, value } = this.props; |
|
|
|
if (this.countdownId) { |
|
|
|
clearInterval(this.countdownId); |
|
|
|
this.countdownId = undefined; |
|
|
|
|
|
|
|
const timestamp = getTime(value); |
|
|
|
if (onFinish && timestamp < Date.now()) { |
|
|
|
onFinish(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
formatCountdown = (value: countdownValueType, config: FormatConfig) => { |
|
|
|