-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus_timer.js
118 lines (98 loc) · 4.12 KB
/
focus_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
// script.js
document.addEventListener('DOMContentLoaded', function() {
const timerForm = document.getElementById('timer-form');
const timerDisplay = document.getElementById('timer-display');
const pauseButton = document.getElementById('pause-button');
const resumeButton = document.getElementById('resume-button');
const alarmSound = document.getElementById('alarm-sound');
const progressBarFill = document.getElementById('progress-bar-fill');
let interval;
let remainingTime;
let isPaused = false;
alarmSound.loop = true;
timerForm.addEventListener('submit', function(event) {
event.preventDefault();
const minutes = parseInt(document.getElementById('minutes').value);
const seconds = parseInt(document.getElementById('seconds').value);
if (isNaN(minutes) || isNaN(seconds) || (minutes <= 0 && seconds <= 0)) {
alert('Please enter a valid number of minutes and seconds.');
return;
}
startTimer(minutes * 60 + seconds);
});
pauseButton.addEventListener('click', function() {
if (!isPaused) {
clearInterval(interval);
isPaused = true;
pauseButton.disabled = true;
resumeButton.disabled = false;
}
});
resumeButton.addEventListener('click', function() {
if (isPaused) {
startTimer(remainingTime);
isPaused = false;
pauseButton.disabled = false;
resumeButton.disabled = true;
}
});
function startTimer(seconds) {
remainingTime = seconds;
timerDisplay.textContent = formatTime(remainingTime);
updateProgressBar(remainingTime, seconds); // Initialize the progress bar
pauseButton.disabled = false;
resumeButton.disabled = true;
interval = setInterval(() => {
remainingTime--;
timerDisplay.textContent = formatTime(remainingTime);
updateProgressBar(remainingTime, seconds);
if (remainingTime <= 0) {
clearInterval(interval);
//timerDisplay.textContent = 'Time is up!';
alarmSound.play();
showAlertWithStopButton();
pauseButton.disabled = true;
resumeButton.disabled = true;
}
}, 1000);
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
function updateProgressBar(remainingTime, totalTime) {
const radius = 140; // Radius of the circle
const circumference = 2 * Math.PI * radius;
const percentage = (remainingTime / totalTime) * 100;
const offset = circumference - (percentage / 100) * circumference;
progressBarFill.style.strokeDashoffset = offset;
}
function showAlertWithStopButton() {
const alertContainer = document.createElement('div');
alertContainer.className = 'alert-container';
const alertMessage = document.createElement('p');
alertMessage.textContent = 'Focus time is over!';
alertMessage.style.marginBottom = '20px';
const stopButton = document.createElement('button');
stopButton.textContent = 'Stop Alarm';
stopButton.style.padding = '10px 20px';
stopButton.style.background = '#dc3545';
stopButton.style.color = 'white';
stopButton.style.border = 'none';
stopButton.style.borderRadius = '5px';
stopButton.style.cursor = 'pointer';
stopButton.addEventListener('click', function() {
alarmSound.pause();
alarmSound.currentTime = 0;
document.body.removeChild(alertContainer);
//timerDisplay.textContent = 'Set a timer';
// Clear input fields
document.getElementById('minutes').value = '';
document.getElementById('seconds').value = '';
});
alertContainer.appendChild(alertMessage);
alertContainer.appendChild(stopButton);
document.body.appendChild(alertContainer);
}
});