-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (126 loc) · 4.35 KB
/
script.js
File metadata and controls
149 lines (126 loc) · 4.35 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
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
146
147
148
149
let sec = 0;
let min = 0;
let hr = 0;
let timer = false;
let exercises = [];
let currentExerciseIndex = 0;
let countdownTimers = [];
let remainingTimes = [];
let isPaused = [];
function show() {
/*document.getElementById("btn1").style.display = "none";*/
document.getElementById("stopwatch").style.display = "block";
}
function startStopwatch() {
if (!timer) {
timer = true;
setInterval(() => {
sec++;
if (sec === 60) {
sec = 0;
min++;
if (min === 60) {
min = 0;
hr++;
}
}
let updatedTime =
(hr < 10 ? '0' + hr : hr) + ':' +
(min < 10 ? '0' + min : min) + ':' +
(sec < 10 ? '0' + sec : sec);
document.getElementById("stopwatch").innerHTML = updatedTime;
}, 1000);
}
}
function addexercise() {
const exerciseName = document.getElementById("Textbox").value;
const exerciseTime = document.getElementById("Timebox").value;
if (exerciseName && validateTime(exerciseTime)) {
exercises.push({ name: exerciseName, time: exerciseTime });
document.getElementById("Textbox").value = '';
document.getElementById("Timebox").value = '';
isPaused.push(false);
remainingTimes.push(0);
displayExercises();
} else {
alert("Please enter a valid exercise name and time in HH:MM:SS format.");
}
}
function displayExercises() {
const output = document.getElementById("output");
output.innerHTML = '';
exercises.forEach((exercise, index) => {
output.innerHTML += `
<div id="exercise-${index}">
Exercise: ${exercise.name}, Time: ${exercise.time}
<span id="countdown-${index}">${formatTime(remainingTimes[index])}</span>
<button class="pause" onclick="pauseExercise(${index})">${isPaused[index] ? 'Resume' : 'Pause'}</button>
</div>
`;
});
}
function formatTime(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `Countdown: ${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
function startWorkout() {
if (exercises.length === 0) {
alert("Please add at least one exercise before starting the workout.");
return;
}
currentExerciseIndex = 0;
show();
startStopwatch();
startExercise(currentExerciseIndex);
}
function startExercise(index) {
if (index < exercises.length) {
const timeParts = exercises[index].time.split(':').map(Number);
let totalSeconds = timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2];
if (remainingTimes[index] === 0) {
remainingTimes[index] = totalSeconds;
}
const countdownElement = document.getElementById(`countdown-${index}`);
countdownElement.innerHTML = formatTime(remainingTimes[index]);
countdownTimers[index] = setInterval(() => {
if (remainingTimes[index] <= 0) {
clearInterval(countdownTimers[index]);
alert(`${exercises[index].name} time is up!`);
currentExerciseIndex++;
if (currentExerciseIndex >= exercises.length) {
stopStopwatch();
return;
}
startExercise(currentExerciseIndex);
} else if (!isPaused[index]) {
remainingTimes[index]--;
countdownElement.innerHTML = formatTime(remainingTimes[index]);
}
}, 1000);
}
}
function stopStopwatch() {
timer = false;
}
function pauseExercise(index) {
if (isPaused[index]) {
isPaused[index] = false;
alert(`Resumed: ${exercises[index].name}.`);
startExercise(index);
} else {
clearInterval(countdownTimers[index]);
isPaused[index] = true;
alert(`Paused: ${exercises[index].name}. You can resume later.`);
}
displayExercises();
}
function validateTime(time) {
const regex = /^\d{2}:\d{2}:\d{2}$/;
return regex.test(time);
}
function endSession() {
localStorage.setItem('exerciseSummary', JSON.stringify(exercises));
window.location.href = 'sum.html';
}