ding-dong
constructor(props){
        super(props);
        this.state = {
            date: Date.now()//获取当前时间戳
        };
    }

    componentDidMount() {
        this.timerID = setInterval(
            () =>this.tick(),
            1000
        );//设置定时器
    }

    componentWillUnmount(){
        clearInterval(this.timerID);//清除定时器
    }

    tick(){
        this.setState({
            date: Date.now()
        });
    }
    render() {

        const { card } = this.props;
        const { date } = this.state;
        const time = date - moment(card.startTime).valueOf();//计算时间差
        let h = Math.floor(time / 1000 / 60 / 60 % 24);//获取时
        let m = Math.floor(time / 1000 / 60 % 60);//获取分
        let s = Math.floor(time / 1000 % 60);//获取秒
    
        if(s < 10) {
            s = "0" + s;
        }
        if(m < 10) {
            m = "0" + m;
        }
        if(h < 10) {
            h = "0" + h;
        }//判断是否是个位数,自动补\'0\'

 

分类:

技术点:

相关文章:

  • 2021-06-15
  • 2021-12-26
  • 2021-11-30
  • 2021-12-09
  • 2021-11-03
  • 2021-11-13
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-12-26
  • 2021-05-30
  • 2022-12-23
相关资源
相似解决方案