Skip to content

Commit

Permalink
feat: Improve player turn timer in GameUI
Browse files Browse the repository at this point in the history
- Clear the player turn timer if it is already running before starting a new turn.
- Encapsulate the timer logic within the `GameUI` class for better organization and reusability.
  • Loading branch information
TKanX committed Aug 28, 2024
1 parent cb16eb1 commit 3f59d1b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions public/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,21 +587,25 @@ class GameUI {
}%, gray ${100 - percentage}% 100%)`;
};

if (this.playerTurnTimer) {
clearInterval(this.playerTurnTimer);
}

const startTime = Date.now();
const timer = setInterval(() => {
this.playerTurnTimer = setInterval(() => {
const elapsedTime = Date.now() - startTime;
const percentage = (elapsedTime / timeout) * 100;
updateBorderPercentage(percentage);

if (elapsedTime >= timeout) {
clearInterval(timer);
clearInterval(this.playerTurnTimer);
updateBorderPercentage(100);
}
}, 100);

return {
stop: () => {
clearInterval(timer);
clearInterval(this.playerTurnTimer);
updateBorderPercentage(100);
},
};
Expand Down

0 comments on commit 3f59d1b

Please sign in to comment.