Skip to content

Commit

Permalink
fix: use reference for deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Mar 21, 2024
1 parent 47bb4a7 commit 620f89b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/timer/TimerProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,20 @@ const TimerProvider = ({
timeLimitMins,
]);

// Set deadline as a reference to timerEnds that updates when it changes
const deadline = useRef(new Date(timerEnds));
useEffect(() => {
deadline.current = new Date(timerEnds);
}, [timerEnds]);

useEffect(() => {
const timerRef = { current: true };
let timerTick = -1;
const deadline = new Date(timerEnds);

const ticker = () => {
timerTick++;
const now = Date.now();
const remainingTime = (deadline.getTime() - now) / 1000;
const remainingTime = (deadline.current.getTime() - now) / 1000;
const secondsLeft = Math.floor(remainingTime);

setTimeState(getFormattedRemainingTime(secondsLeft));
Expand Down Expand Up @@ -142,7 +147,7 @@ const TimerProvider = ({
clearInterval(timerRef.current);
timerRef.current = null;
};
}, [ // eslint-disable-line react-hooks/exhaustive-deps
}, [
pingInterval,
workerUrl,
processTimeLeft,
Expand Down

0 comments on commit 620f89b

Please sign in to comment.