时钟不随采样时间输入滴答(不获取系统时间)

我想从x日期/时间到y日期/时间启动倒数计时器。 输出应该每秒变化。 如果我想以静态格式使用它,但代码运行良好,但是我希望它是动态的。 时间应该流逝,而无需任何人重新加载。

我是Javascript和React的新手。需要一些帮助。

target

我希望输出 import React,{ Component } from 'react'; export class Clock extends Component { constructor(props) { super(props); let fixDate = new Date("December 8,2019 00:00:00"); let currDate = new Date("December 4,2019 00:00:00"); this.state = { fixDate,diff: fixDate - currDate }; } componentDidmount() { this.timerID = setInterval(() => this.tick(),1000); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ currDate: new Date(December 4,2019 00:00:00") }); } render() { const { diff } = this.state; const days = Math.abs(Math.floor(diff / (24 * 60 * 60 * 1000))); const totalhours = Math.floor(diff / (60 * 60 * 1000)); const plusminus = totalhours % 24; const hours = Math.abs(totalhours % 24); const mins = Math.floor((diff - (totalhours * 60 * 60 * 1000)) / (60 * 1000)); const secs = Math.floor((diff - (totalhours * 60 * 60 * 1000) - (mins * 60 * 1000)) / 1000); if (plusminus < 0) { return ( <div> <h2>Past {days} Days {hours} Hours {mins} Mins {secs} Secs</h2> </div> ); } else { return ( <div> <h2>{days} Days {hours} Hours {mins} Mins {secs} Secs to go</h2> </div> ); } } } , 但是它应该每秒改变一次。

sb3006 回答:时钟不随采样时间输入滴答(不获取系统时间)

请注意,您未更改状态中的diff的值。 diff仅在构造函数中设置。

输出不会每秒更改一次,因为在每个tick()上您都没有更改currDate的值。

因此,相反,我们可以在渲染组件之前设置currDate并找到diff的值。

请参阅下面的示例(尽管它与4天的预期输出不匹配!)。

class Clock extends React.Component {
    constructor(props) {
        super(props);
        let fixDate = new Date("December 8,2019 00:00:00"); 
        let currDate = new Date();//"December 4,2019 00:00:00");
        this.state = { fixDate,currDate };
    }
    componentDidMount() {
        this.timerID = setInterval(() => this.tick(),1000);
    }
    componentWillUnmount() {
        clearInterval(this.timerID);
    }
    tick() {
        this.setState({
            currDate: new Date()//"December 4,2019 00:00:00")
        });
    }
    render() {
        const { fixDate,currDate } = this.state;
     	const diff = fixDate - currDate;
      	//console.log(diff);
        const days = Math.abs(Math.floor(diff / (24 * 60 * 60 * 1000)));
        const totalhours = Math.floor(diff / (60 * 60 * 1000));
        const plusminus = totalhours % 24;
        const hours = Math.abs(totalhours % 24);
        const mins = Math.floor((diff - (totalhours * 60 * 60 * 1000)) / (60 * 1000));
        const secs = Math.floor((diff - (totalhours * 60 * 60 * 1000) - (mins * 60 * 1000)) / 1000);
        if (plusminus < 0) {
            return (
                <div>
                    <h2>Past {days} Days {hours} Hours {mins} Mins {secs} Secs</h2>
                </div>
            );
        } else {
            return (
                <div>
                    <h2>{days} Days {hours} Hours {mins} Mins {secs} Secs to go</h2>
                </div>
            );
        }
    }
}

ReactDOM.render(
  <Clock></Clock>,document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="container">
</div>

本文链接:https://www.f2er.com/3167392.html

大家都在问