-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTimer.js
34 lines (32 loc) · 820 Bytes
/
Timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Created by tdzl2003 on 12/17/16.
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class Timer extends Component {
static propTypes = {
interval: PropTypes.number,
onTimer: PropTypes.func,
children: PropTypes.element,
};
componentDidMount() {
const { interval } = this.props;
this.timer = setInterval(this.onEvent, interval);
}
componentDidUpdate(prevProps) {
if (prevProps.interval !== this.props.interval) {
clearInterval(this.timer);
this.timer = setInterval(this.onEvent, this.props.interval);
}
}
componentWillUnmount() {
clearInterval(this.timer);
}
onEvent = ev => {
const { onTimer } = this.props;
onTimer && onTimer(ev);
};
render() {
return this.props.children || null;
}
}