-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
34 lines (28 loc) · 884 Bytes
/
timer.js
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
let timers = {};
let intervals = {};
function startTimer(timerId) {
if (intervals[timerId]) {
return; // Timer is already running
}
if (!timers[timerId]) {
timers[timerId] = 0;
}
intervals[timerId] = setInterval(() => {
timers[timerId]++;
document.getElementById(timerId).textContent = formatTime(timers[timerId]);
}, 1000);
}
function stopTimer(timerId) {
clearInterval(intervals[timerId]);
intervals[timerId] = null;
}
function resetTimer(timerId) {
stopTimer(timerId);
timers[timerId] = 0;
document.getElementById(timerId).textContent = "00:00";
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}