我想从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>
);
}
}
}
,
但是它应该每秒改变一次。