From 506fbae9a3ba43c1a205471d0485bc80ebd920fa Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Mon, 17 Jun 2024 09:59:16 -0400 Subject: [PATCH 1/5] Create counter interval that starts at 10 --- src/components/Question.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/Question.js b/src/components/Question.js index 483506fb..bed09a4c 100644 --- a/src/components/Question.js +++ b/src/components/Question.js @@ -1,9 +1,16 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; function Question({ question, onAnswered }) { const [timeRemaining, setTimeRemaining] = useState(10); // add useEffect code + useEffect(() => { + console.log("useEffect called") + }) + const timerId = setTimeout(() => { + setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) + + function handleAnswer(isCorrect) { setTimeRemaining(10); From 74edf745c6946438ed8e7ee96607ac4c65ee97a9 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Mon, 17 Jun 2024 10:23:47 -0400 Subject: [PATCH 2/5] Resets timer to 10 when it hit zero --- src/components/Question.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/components/Question.js b/src/components/Question.js index bed09a4c..64601587 100644 --- a/src/components/Question.js +++ b/src/components/Question.js @@ -5,10 +5,20 @@ function Question({ question, onAnswered }) { // add useEffect code useEffect(() => { - console.log("useEffect called") - }) - const timerId = setTimeout(() => { - setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) + if (timeRemaining === 0) { + setTimeRemaining(10) + + } + const intervalId = setTimeout(() => { + console.log("running") + setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) + + return function() { + clearInterval(intervalId) + } + + }, [timeRemaining]) + From ec460c394981446434610edd04df6fef334df20a Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Mon, 17 Jun 2024 10:34:04 -0400 Subject: [PATCH 3/5] Change question after timer hits zero --- src/components/Question.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/components/Question.js b/src/components/Question.js index 64601587..496c9c89 100644 --- a/src/components/Question.js +++ b/src/components/Question.js @@ -2,22 +2,21 @@ import React, { useState, useEffect } from "react"; function Question({ question, onAnswered }) { const [timeRemaining, setTimeRemaining] = useState(10); - // add useEffect code useEffect(() => { if (timeRemaining === 0) { setTimeRemaining(10) - + onAnswered(false) } + const intervalId = setTimeout(() => { - console.log("running") setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) - return function() { - clearInterval(intervalId) - } + // return function() { + // clearInterval(intervalId) + // } - }, [timeRemaining]) + }, [timeRemaining, onAnswered]) From c8d9456d21e65e8f1fb6103d70a7c55152d4cb6e Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Mon, 17 Jun 2024 10:37:39 -0400 Subject: [PATCH 4/5] Created clean up function --- src/components/Question.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Question.js b/src/components/Question.js index 496c9c89..afcd1e1d 100644 --- a/src/components/Question.js +++ b/src/components/Question.js @@ -12,9 +12,9 @@ function Question({ question, onAnswered }) { const intervalId = setTimeout(() => { setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) - // return function() { - // clearInterval(intervalId) - // } + return function() { + clearInterval(intervalId) + } }, [timeRemaining, onAnswered]) From c8d503a5d4a8c638fca84aa6cd9094e7a66c437f Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Mon, 17 Jun 2024 10:47:11 -0400 Subject: [PATCH 5/5] Add ClearTimeout to pass test --- src/components/Question.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/components/Question.js b/src/components/Question.js index afcd1e1d..c463ded9 100644 --- a/src/components/Question.js +++ b/src/components/Question.js @@ -2,24 +2,21 @@ import React, { useState, useEffect } from "react"; function Question({ question, onAnswered }) { const [timeRemaining, setTimeRemaining] = useState(10); - // add useEffect code + useEffect(() => { if (timeRemaining === 0) { - setTimeRemaining(10) - onAnswered(false) + setTimeRemaining(10); + onAnswered(false); } const intervalId = setTimeout(() => { - setTimeRemaining(timeRemaining => timeRemaining -1 )}, 1000) - - return function() { - clearInterval(intervalId) - } - - }, [timeRemaining, onAnswered]) - - + setTimeRemaining((timeRemaining) => timeRemaining - 1); + }, 1000); + return () => { + clearTimeout(intervalId); + }; + }, [timeRemaining, onAnswered]); function handleAnswer(isCorrect) { setTimeRemaining(10); @@ -46,3 +43,4 @@ function Question({ question, onAnswered }) { } export default Question; +