Add a Timer to the score board
consci210 opened this issue · 4 comments
It keeps given me an error when creating the app
@Mublin Can you send me a detailed info . Tell me what you did and at what step you encountered an error
@Mublin Can you send me a detailed info . Tell me what you did and at what step you encountered an error
@consci210 the bug is the timer actually works but I am missing a logic
I created a variable in the DataContext of time = 30
I created a function that is called every 1 second using setInterval and once it get to 0 it moves to the next question but the problem is; I cannot access the questionIndex state when it changes the variable keeps staying the same and I need it because I want the index reach 9;
After the timer reaches 0, I want to use ClearInterval.
should I make it like the overall time?
Like let the countdown start from 5minutes or something like that?
@Mublin Oh I see ,I don't think its a good idea to access the question indexes though, as that would mean you also need to modify many other functions as well , You can simply call the handleNextQuestion
function once the timer reaches to zero . and then you can set the timer to be 30 again in order for it to start counting again for the next question .
try using a code like this :
const [timer, setTimer] = useState(30);
const countdownRef = useRef(null);
useEffect(() => {
countdownRef.current = setInterval(() => {
setTimer((prevTimer) => {
if (prevTimer === 1) {
clearInterval(countdownRef.current);
handleNextQuestion();
return 30;
} else {
return prevTimer - 1;
}
});
}, 1000);
return () => clearInterval(countdownRef.current);
}, [handleNextQuestion]);
You might need to add minor tweaks to one or two functions , like the getAnswerChoices
function as it is called every time the page re-renders and since we don't want that to happen .