-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
145 lines (135 loc) · 4.71 KB
/
Copy pathindex.html
File metadata and controls
145 lines (135 loc) · 4.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meditation Timer</title>
<style>
body {
background-color: black;
color: white;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: 'Courier New', monospace;
position: relative;
}
#timer {
font-size: 5rem;
cursor: pointer;
}
#controls {
position: absolute;
top: 10px;
right: 10px;
font-size: 2rem;
}
.control-button {
cursor: pointer;
margin-left: 10px;
}
</style>
</head>
<body>
<div id="controls">
<span id="play-pause" class="control-button">🟢</span>
<span id="ring-bell" class="control-button">🔔</span>
<span id="set-timer" class="control-button">⏲️</span>
</div>
<div id="timer"></div>
<audio id="bell-sound" src="bell.mp3"></audio>
<script>
let countdownMinutes = 25; // Set timer duration here
let countdownSeconds = 0;
let isRunning = false;
let countdownInterval;
let targetEndTime;
const timerElement = document.getElementById("timer");
const bellSound = document.getElementById("bell-sound");
const playPauseButton = document.getElementById("play-pause");
const ringBellButton = document.getElementById("ring-bell");
const setTimerButton = document.getElementById("set-timer");
function playBell(times, partialDuration = 1500) {
let count = 0;
function play() {
if (count < times) {
bellSound.currentTime = 0.888;
bellSound.play().catch(handleAudioError);
count++;
if (count < times) {
setTimeout(() => {
if (count < times - 1) {
bellSound.pause();
}
play();
}, partialDuration);
}
}
}
play();
}
function handleAudioError(error) {
console.error("Audio playback was prevented by the browser:", error);
}
function updateTimerDisplay() {
if (targetEndTime) {
const now = new Date().getTime();
const timeRemaining = Math.max(targetEndTime - now, 0);
countdownMinutes = Math.floor(timeRemaining / (1000 * 60));
countdownSeconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
}
const minutes = countdownMinutes.toString().padStart(2, '0');
const seconds = countdownSeconds.toString().padStart(2, '0');
timerElement.textContent = `${minutes}:${seconds}`;
}
function startCountdown() {
targetEndTime = new Date().getTime() + countdownMinutes * 60000 + countdownSeconds * 1000;
countdownInterval = setInterval(() => {
updateTimerDisplay();
if (countdownMinutes === 0 && countdownSeconds === 0) {
clearInterval(countdownInterval);
playPauseButton.textContent = '🟢'; // Change to play icon when timer ends
bellSound.currentTime = 0.888;
bellSound.play().catch(handleAudioError);
}
}, 250);
}
function toggleTimer() {
if (isRunning) {
clearInterval(countdownInterval);
isRunning = false;
playPauseButton.textContent = '🟢'; // Change to play icon
} else {
if (!targetEndTime) { // Only play the bell if it's the initial start
playBell(3);
}
startCountdown();
isRunning = true;
playPauseButton.textContent = '⏸️'; // Change to pause icon
}
}
function ringBellNow() {
playBell(1);
}
function setNewTimer() {
const newTime = prompt("Enter new time in MM:SS format:", "25:00");
if (newTime) {
const [newMinutes, newSeconds] = newTime.split(":").map(Number);
if (!isNaN(newMinutes) && !isNaN(newSeconds)) {
countdownMinutes = newMinutes;
countdownSeconds = newSeconds;
targetEndTime = null; // Reset target end time
updateTimerDisplay();
} else {
alert("Invalid time format. Please use MM:SS.");
}
}
}
playPauseButton.addEventListener("click", toggleTimer);
ringBellButton.addEventListener("click", ringBellNow);
setTimerButton.addEventListener("click", setNewTimer);
updateTimerDisplay();
</script>
</body>
</html>