oeeDownCountTime.js 2.16 KB
/* 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;