https://ko.javascript.info/settimeout-setinterval
https://ko.reactjs.org/docs/hooks-effect.html
https://handhand.tistory.com/32
// 2초 간격으로 메시지를 보여줌
let timerId = setInterval(() => alert('째깍'), 2000);
// 5초 후에 정지
setTimeout(() => { clearInterval(timerId); alert('정지'); }, 5000);
import React, { useState, useEffect, useRef } from "react";
const GameForm = () => {
const [sec, setSec] = useState(12);
const time = useRef(12);
const timerId = useRef(null);
const [score, setScore] = useState(0);
const [answers, setAnswers] = useState([false, false, false, false]);
//타이머
useEffect(() => {
timerId.current = setInterval(() => {
setSec(parseInt(time.current));
time.current -= 1;
}, 1000);
return () => clearInterval(timerId.current);
}, []);
//타임아웃
useEffect(() => {
if (time.current < 0) {
console.log("타임 아웃");
clearInterval(timerId.current);
}
}, [sec]);
//답안부분
const onClickAnswer = (e) => {
let copy = [...answers];
setNum(parseInt(number.current));
number.current = number.current + 1;
const chooseAnswer = parseInt(e.currentTarget.value);
setShowScore(false);
// console.log("chooseAnswer", chooseAnswer);
// console.log("gameLists[num].answer", gameLists[num].answer);
if (chooseAnswer === gameLists[num].answer) {
setShowScore(true);
setScore((score += 10));
console.log("정답!");
copy[0] = false;
copy[1] = false;
copy[2] = false;
copy[3] = false;
if (chooseAnswer === 1) {
copy[0] = true;
setAnswers(copy);
} else if (chooseAnswer === 2) {
copy[1] = true;
setAnswers(copy);
} else if (chooseAnswer === 3) {
copy[2] = true;
setAnswers(copy);
} else if (chooseAnswer === 4) {
copy[3] = true;
setAnswers(copy);
}
} else {
console.log("오답!");
}
//timer를 처음 값으로 되돌림
time.current = 12;
setSec(parseInt(time.current));
console.log("copy", copy);
console.log("answers", answers);
console.log("score", score);
};
return (
{/* time start */}
<div className="bg-white shadow-lg p-1 rounded-full w-full h-5 mt-4">
//해당 부분 수정
<div
className={`bg-light-green rounded-full h-full
${sec === 11 && "w-11/12"}
${sec === 10 && "w-10/12"}
${sec === 9 && "w-9/12"}
${sec === 8 && "w-8/12"}
${sec === 7 && "w-7/12"}
${sec === 6 && "w-6/12"}
${sec === 5 && "w-5/12"}
${sec === 4 && "w-4/12"}
${sec === 3 && "w-3/12"}
${sec === 2 && "w-2/12"}
${sec === 1 && "w-1/12"}
${sec === 0 && "w-0"}`}
></div>
시간: {sec}
</div>
{/* time end */}
{/* 버튼 부분 start */}
//각각 value값과 gameLists[num].answer를 비교
<button
value="1" //1, 2, 3, 4
onClick={onClickAnswer}
className={`w-full bg-light-beige p-2 rounded-lg mb-3 relative`}
>
<div className="rounded-lg font-bold flex ">
<div className="bg-white p-3 rounded-lg">1</div>
<div
id="point"
className="flex items-center pl-10 w-full text-lg"
value={gameLists[`${num}`]?.answer}
>
{gameLists[`${num}`]?.choices[0]}
//answers[0], answers[1], answers[2], answers[3]
{answers[0] ? (
<div className="bg-light-orange pt-2 pl-1 transform rotate-45 rounded-full h-12 w-12 text-white font-bold absolute right-0 bottom-8 shadow-md">
<p className="transform -rotate-45 text-black text-lg">
+10
</p>
</div>
) : null}
</div>
</div>
</button>
{/* 버튼 부분 end */}
);
};
export default GameForm;