grafana/public/app/features/explore/ElapsedTime.tsx

47 lines
941 B
TypeScript
Raw Normal View History

2018-04-26 04:58:42 -05:00
import React, { PureComponent } from 'react';
const INTERVAL = 150;
export default class ElapsedTime extends PureComponent<any, any> {
offset: number;
2018-04-27 04:49:11 -05:00
timer: number;
2018-04-26 04:58:42 -05:00
state = {
elapsed: 0,
};
start() {
this.offset = Date.now();
2018-04-27 04:49:11 -05:00
this.timer = window.setInterval(this.tick, INTERVAL);
2018-04-26 04:58:42 -05:00
}
tick = () => {
const jetzt = Date.now();
const elapsed = jetzt - this.offset;
this.setState({ elapsed });
};
componentWillReceiveProps(nextProps) {
if (nextProps.time) {
clearInterval(this.timer);
} else if (this.props.time) {
this.start();
}
}
componentDidMount() {
this.start();
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
const { elapsed } = this.state;
const { className, time } = this.props;
const value = (time || elapsed) / 1000;
return <span className={`elapsed-time ${className}`}>{value.toFixed(1)}s</span>;
2018-04-26 04:58:42 -05:00
}
}