Skip to content

Commit 484c32b

Browse files
committed
refactor to use setInterval again
1 parent 4a74b33 commit 484c32b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

modules/timer/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,34 @@ type State = {
4242

4343
export class Timer extends React.Component<Props, State> {
4444
_timeoutId: ?TimeoutID
45+
_intervalId: ?IntervalID
4546

4647
state = {
4748
now: new Date(),
4849
loading: false,
4950
}
5051

5152
componentDidMount() {
52-
this.queue()
53-
}
54-
55-
componentWillUnmount() {
56-
// eslint-disable-next-line no-eq-null
57-
this._timeoutId != null && clearTimeout(this._timeoutId)
58-
}
59-
60-
queue = () => {
6153
// get the time remaining until the next $interval
6254
let {interval} = this.props
6355
let nowMs = this.state.now.getTime()
6456
let untilNextInterval = msUntilIntervalRepeat(nowMs, interval)
6557

66-
this._timeoutId = setTimeout(this.updateTime, untilNextInterval)
58+
this._timeoutId = setTimeout(() => {
59+
this.updateTime()
60+
this._intervalId = setInterval(this.updateTime, interval)
61+
}, untilNextInterval)
62+
}
63+
64+
componentWillUnmount() {
65+
// eslint-disable-next-line no-eq-null
66+
this._timeoutId != null && clearTimeout(this._timeoutId)
67+
// eslint-disable-next-line no-eq-null
68+
this._intervalId != null && clearInterval(this._intervalId)
6769
}
6870

6971
updateTime = () => {
70-
this.setState(() => ({now: new Date()}), this.queue)
72+
this.setState(() => ({now: new Date()}))
7173
}
7274

7375
refresh = async () => {

0 commit comments

Comments
 (0)