-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
145 lines (125 loc) · 4.64 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var timerElement = document.getElementById("timer");
var startButton = document.getElementById("start-button");
var pauseButton = document.getElementById("pause-button");
var nextButton = document.getElementById("next-button");
var endButton = document.getElementById("end-button");
var audio = new Audio("ding.mp3"); // Create the audio element
var sections = [
{ title: "VR", duration: 1260 }, // 21 minutes = 21 * 60 seconds
{ title: "DM", duration: 1860 }, // 31 minutes = 31 * 60 seconds
{ title: "QR", duration: 1500 }, // 25 minutes = 25 * 60 seconds
{ title: "AR", duration: 720 }, // 12 minutes = 12 * 60 seconds
{ title: "SJ", duration: 1560 } // 26 minutes = 26 * 60 seconds
];
var breakDuration = 60; // 1 minute
var currentSectionIndex = 0;
var isBreak = false;
var isPaused = false;
var timeRemaining = sections[currentSectionIndex].duration;
var timerInterval;
// Display the duration of the first section (VR)
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
timerElement.innerText = formatTime(minutes) + ":" + formatTime(seconds);
function startTimer() {
startButton.disabled = true;
pauseButton.disabled = false;
nextButton.disabled = false;
endButton.disabled = false; // Make end button active
clearInterval(timerInterval);
timerInterval = setInterval(updateTimer, 1000);
}
function pauseTimer() {
if (!isPaused) {
clearInterval(timerInterval);
isPaused = true;
pauseButton.innerText = "Resume";
} else {
timerInterval = setInterval(updateTimer, 1000);
isPaused = false;
pauseButton.innerText = "Pause";
}
}
function updateTimer() {
timeRemaining--;
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
timerElement.innerText = formatTime(minutes) + ":" + formatTime(seconds);
if (timeRemaining === 0) {
audio.play();
clearInterval(timerInterval);
timerElement.innerText = "00:00";
// If in a break or at the last section, move to the next section
if (isBreak || currentSectionIndex === sections.length - 1) {
nextSection();
} else { // Otherwise, introduce a break
timeRemaining = breakDuration;
isBreak = true;
displaySectionTitle();
startButton.disabled = false;
pauseButton.disabled = true;
nextButton.disabled = false;
endButton.disabled = false;
// Display the break time immediately
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
timerElement.innerText = formatTime(minutes) + ":" + formatTime(seconds);
startTimer(); // Automatically start the timer
}
}
}
function displaySectionTitle() {
var sectionTitle = document.getElementById("section-title");
if (isBreak) {
sectionTitle.innerText = "Break";
} else {
sectionTitle.innerText = sections[currentSectionIndex].title;
}
}
function formatTime(time) {
return time < 10 ? "0" + time : time;
}
function nextSection() {
if (isPaused) {
pauseTimer(); // Resume the timer if paused
}
if (!isBreak) {
// If current section is the last one, restart the program
if (currentSectionIndex === sections.length - 1) {
endProgram();
} else {
timeRemaining = breakDuration;
isBreak = true;
displaySectionTitle();
startTimer(); // Automatically start the timer
}
} else {
currentSectionIndex++;
timeRemaining = sections[currentSectionIndex].duration;
isBreak = false;
displaySectionTitle();
audio.play(); // Play the sound when a new section starts
startTimer(); // Automatically start the timer
}
// Update the timer display
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
timerElement.innerText = formatTime(minutes) + ":" + formatTime(seconds);
}
function endProgram() {
currentSectionIndex = 0;
isBreak = false; // Reset the break flag
timeRemaining = sections[currentSectionIndex].duration;
displaySectionTitle();
startButton.disabled = false;
pauseButton.disabled = true;
nextButton.disabled = false;
endButton.disabled = false;
clearInterval(timerInterval);
timerElement.innerText = "00:00";
// Display the duration of the first section (VR)
var minutes = Math.floor(timeRemaining / 60);
var seconds = timeRemaining % 60;
timerElement.innerText = formatTime(minutes) + ":" + formatTime(seconds);
}
displaySectionTitle();