Search code examples
javascriptreactjsjiraatlassian-forge

Countdown is not updating even with SetInterval


I am trying to create a countdown forge application that takes an input of type date and starts counting based on the actual date. As an example, I am giving as input "After 3 days from now", But I only get as a result, 2d:23h:59m:59s , ans this result is not updating every second.

Here is the main function:

const useCountdown = (targetDate) => {
const countDownDate = new Date(targetDate).getTime();

const [countDown, setCountDown] = useState(
countDownDate - new Date().getTime()
);

useEffect(() => {
const interval = setInterval(() => {
  setCountDown(countDownDate - new Date().getTime());
}, 1000);

return () => clearInterval(interval);
}, [countDownDate]);

return getReturnValues(countDown);
};

This is where I display the countdown:

const Edit = () => {

const THREE_DAYS_IN_MS = 3 * 24 * 60 * 60 * 1000;
const NOW_IN_MS = new Date().getTime();

const dateTimeAfterThreeDays = NOW_IN_MS + THREE_DAYS_IN_MS;
 return(
  <Fragment>
  <Text>Time left 123:</Text>
  <CountdownTimer targetDate={dateTimeAfterThreeDays} />
  </Fragment>
   )
       };
    export const renderFieldView = render(<View />);

I am using Atlassian forge and deploying the app in Jira.


Solution

  • I hope the original poster has found the solution by now, but for anyone coming across this post, I believe part of the reason this isn't working is because it's using UI Kit (original) which uses an Atlassian implementation of useEffect that doesn't work quite the same way as React.

    I've written a bit more about how Forge uses hooks in each of the different kinds of apps in this blog https://blog.developer.atlassian.com/a-deeper-look-at-hooks-in-forge/

    I can confirm, if you build this using UI Kit 2 (which uses React hooks) then it will work.