I am using react-countdown library for displaying a timer on UI.
I am trying to pass server time in milliseconds to now prop of countdown timer but it is not updating the timer ticks .And if i'm passing Date.now() in that case it works properly.
I have defined an funcion that returns me server time regardless of the user's system time. And i'm trying to pass this updated time to now prop. But the timer won't tick/update.
As per the react-countdown doc: https://www.npmjs.com/package/react-countdown we can pass our custom time to now prop. But this is not working.
Any help will be highly appreciated !
const getCurrentTime = () => {
let clientDateTime = moment.utc();
let serverDateTime = moment("2021-01-29T12:25:45.682793Z");
let timediff = serverDateTime.diff(clientDateTime, "minutes", true);
let currentTime = moment(new Date());
let updatedDateTime: any;
if (Math.sign(timediff) === 0) {
updatedDateTime = currentTime;
} else {
updatedDateTime = moment(currentTime).add(timediff, "minutes");
}
return new Date(updatedDateTime).valueOf();
};
return (
<Fragment>
<Countdown
onComplete={(delta: CountdownTimeDelta) => {
if (typeof options?.onComplete === "function") {
return options.onComplete(delta);
}
}}
date={remainingTicks}
renderer={renderer}
now={() => getCurrentTime()}
autoStart={true}
/>
</Fragment>
);
question from:
https://stackoverflow.com/questions/65952312/react-countdown-countdown-is-not-ticking-when-i-pass-custom-date-to-now-prop 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…