Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - React countdown : Countdown is not ticking when I pass custom date to now prop

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...