-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
50 lines (45 loc) · 1.39 KB
/
timer.js
File metadata and controls
50 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let timer1 = 300;
let timer2 = 300;
let interval;
const time1Element = document.getElementById('time1');
const time2Element = document.getElementById('time2');
const startBtn = document.getElementById('start');
const pauseBtn = document.getElementById('pause');
const resetBtn = document.getElementById('reset');
function updateTime() {
time1Element.textContent = formatTime(timer1);
time2Element.textContent = formatTime(timer2);
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
function startTimer() {
if (!interval) {
interval = setInterval(() => {
if(!ispaused){
if (!isBlackTurn) {
timer1--;
if (timer1 <= 0) {
clearInterval(interval);
diatext.innerText = 'Time is up, Black wins';
dialog.showModal();
}
} else {
timer2--;
if (timer2 <= 0) {
clearInterval(interval);
diatext.innerText = 'Time is up, White wins';
dialog.showModal();
}
}
updateTime();
}
}, 1000);
}
}
function pauseTimer() {
clearInterval(interval);
interval = null;
}