oeeDownCountTime.js
2.16 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint-disable */
import moment from "moment";
import React, { Component } from "react";
// import { Form, Layout } from 'antd';
import "../../index.less";
import * as commonUtils from "../../utils/utils";
// eslint-disable-next-line no-unused-vars
let countDown = null;
class OeeDownCountTime extends Component {
constructor(props) {
super(props);
this.state = {
oldDate: null,
totalSeacond: 0
};
}
componentWillMount() {
this.mounted = true;
}
componentWillReceiveProps(nextProps) {
const { masterData: masterDataOld, bResetTimer } = nextProps;
// const { oldDate } = this.state;
/* bResetTimer 秒表回置 */
if (
commonUtils.isNotEmptyObject(masterDataOld) &&
(bResetTimer === undefined || bResetTimer)
) {
this.countDownTime();
const oldDate = masterDataOld.tCreateDate;
this.setState({ oldDate });
}
}
componentWillUnmount() {
this.mounted = false;
}
countDownTime = () => {
countDown = setInterval(() => {
const { oldDate } = this.state;
const m1 = moment(new Date());
const m2 = moment(oldDate);
const totalSeacond = m1.diff(m2, "second");
if (this.mounted) {
this.setState({ totalSeacond });
}
}, 1000);
};
timeChange = time => {
let hh;
let mm;
let ss;
// 传入的时间为空或小于0
if (time === null || time < 0) {
return;
}
// 得到小时
// eslint-disable-next-line radix
hh = parseInt(time / 3600);
// eslint-disable-next-line radix,no-mixed-operators
time -= hh * 3600;
// eslint-disable-next-line radix
if (parseInt(hh) < 10) {
hh = `0${hh}`;
}
// 得到分
// eslint-disable-next-line radix
mm = parseInt(time / 60);
// 得到秒
// eslint-disable-next-line radix,no-mixed-operators
ss = time - mm * 60;
// eslint-disable-next-line radix
if (parseInt(mm) < 10) {
mm = `0${mm}`;
}
if (ss < 10) {
ss = `0${ss}`;
}
return `${hh}:${mm}:${ss}`;
};
render() {
const { totalSeacond } = this.state;
return <div>{this.timeChange(totalSeacond)}</div>;
}
}
export default OeeDownCountTime;