generated from muhtadeetaron/nextjs-template
fix(api): exam logic yet to be fixed
This commit is contained in:
@ -23,7 +23,7 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
const [timeRemaining, setTimeRemaining] = useState<number>(0);
|
||||
const timerRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
// countdown effect
|
||||
// Effect: run interval whenever timeRemaining is set > 0
|
||||
useEffect(() => {
|
||||
if (timeRemaining > 0 && !timerRef.current) {
|
||||
timerRef.current = setInterval(() => {
|
||||
@ -39,16 +39,18 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (timerRef.current) {
|
||||
if (timerRef.current && timeRemaining <= 0) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [timeRemaining]);
|
||||
}, [timeRemaining]); // 👈 depend on timeRemaining
|
||||
|
||||
const resetTimer = (duration: number) => {
|
||||
if (timerRef.current) clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
setTimeRemaining(duration);
|
||||
};
|
||||
|
||||
@ -57,11 +59,14 @@ export const TimerProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
setTimeRemaining(0);
|
||||
};
|
||||
|
||||
const setInitialTime = (duration: number) => {
|
||||
if (timerRef.current) clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
if (timerRef.current) {
|
||||
clearInterval(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
setTimeRemaining(duration);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user