Skip to content

Commit

Permalink
Counter only runs when the player must guess
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlc committed Dec 8, 2023
1 parent 949c72c commit 747140e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function App() {
</div>
<div>
<h1>Country Guesser</h1>
<Timer gameRunning={showGameOver === false} />
<Timer gameRunning={showAnswer === null && showGameOver === false} />
</div>
<div>
<div>Streak: {streak}</div>
Expand Down
5 changes: 2 additions & 3 deletions src/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import { useEffect, useLayoutEffect, useRef, useState } from 'react';
export function Timer({ gameRunning }) {
const modal = useModal(IntroModal);
const [isRunning, setIsRunning] = useState(false);
const startTime = useRef(Date.now());

const timerRef = useRef<HTMLParagraphElement>(null);

useEffect(() => {
let intervalId: number;
let startTime = Date.now();
let time = Date.now();
if (isRunning) {
// setting time from 0 to 1 every 10 milisecond using javascript setInterval method
intervalId = setInterval(() => {
time = Date.now() - startTime;
const time = Date.now() - startTime.current;
const hours = Math.floor(time / 3600000);
const minutes = Math.floor((time % 3600000) / 60000);
const seconds = Math.floor((time % 60000) / 1000);
Expand Down

0 comments on commit 747140e

Please sign in to comment.