I’m facing issue in updating the setStartTime
inside setInterval function.
I want to update the startTime
every minute. To update that, I’ve created a useRef
and storing the value in timeRef.current
. Every minute, startTime
is getting updated.
const [startTimeOffsetMins, setStartTimeOffsetMins] = useState(10)
const timeRef = useRef(time)
useEffect(() => {
timeRef.current = time
}, [moment(time).minutes()]);
useEffect(() => {
let tempCurrentTime = moment();
const startTimeOffset = activeTab === 0 ? (startTimeOffsetMins + 1) : (startTimeOffsetMins + 10);
const refreshTimeFunc = () => {
const nowtime = moment();
let startTimeInitial = activeTab === 0 ? moment(timeRef.current).add({h: 0, m: 1}).toDate() : moment().toDate();
if (dynamicStartTime && (moment(tempCurrentTime).minutes() !== moment(nowtime).minutes())) {
setStartTime(moment(startTimeInitial).add({ h: 0, m: startTimeOffset }).set({ s:0, ms:0}).toISOString());
tempCurrentTime = moment(nowtime);
}
}
const refreshTimer = setInterval(refreshTimeFunc, 1000);
return () => {
clearInterval(refreshTimer);
}
}, [activeTab, time, dynamicStartTime]);
Issue: Inside the if condition, startTimeInitial
is getting 1 min delay. So, I added add({h: 0, m: 1})
to compensate.
But considering one scenario: dynamicStartTime
is a toggle button which has boolean. In some scenario when dynamicStartTime
is toggled OFF then ON, an extra 1 minute is added to startTimeInitial
I have read answers which says I can update prevState + 1
. But in this case, how can I update and avoid this situation. I am using moment to update time.
2
Answers
The
tempCurrentTime
andnowTime
should check thetime
variable to see any update. When thetime
value changes every minutes, we can compare old and new minutes. Also, we can remove the (startTimeOffsetMins + 1)Modified code:
Remove the manual minute adjustment.
Use a more precise way to check if a minute has passed by comparing seconds and milliseconds.
Update
startTimeInitial
correctly based on theactiveTab
anddynamicStartTime
state.Here’s the updated code: