What I trying to achieve is, making a countdown to target day from now. inside of componentDidMount()
:
nowT = moment(new Date()).format("X"); // => 1603551057 (2020-10-24)
targetDay = moment(result.data.created_at).add('30', 'days').format("X"); // => 1606143175 (2020-11-23)
diffTime = targetDay - nowT; // => 2592000
remain = moment.duration(diffTime, 'milliseconds'); // => {milliseconds: 0, seconds: 11, minutes: 43, hours: 0, days: 0, …}
let intervalId = setInterval(this.countdown(remain), 1000);
this.setState({
counter: intervalId
});
First, I get now
and targetday
and calculate difference then send remaining to interval. here is countdown
function:
countdown = (r) => {
let remain = moment.duration(r - 1000, 'milliseconds');
this.setState({
currentCount: remain.days() + ':' + remain.hours() + ":" + remain.minutes() + ":" + remain.seconds()
});
console.log(remain.days() + ':' + remain.hours() + ":" + remain.minutes() + ":" + remain.seconds()); // => 0:0:43:11
}
Problem is it return wrong countdown 0:0:43:11
also it not update in render
, just show this static countdown, not dynamic. what I have done wrong?