-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
160 lines (139 loc) · 4.1 KB
/
script.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
let timerInterval;
let remainingTime = 0;
let minutesInput = document.getElementById('minutes');
let startButton = document.getElementById('start');
let pauseButton = document.getElementById('pause');
let resumeButton = document.getElementById('resume');
let resetButton = document.getElementById('reset');
let timerDisplay = document.getElementById('timer');
Notification.requestPermission();
function sendNotification(v1, v2) {
var title = v1;
var body = v2;
new Notification(title, { body });
}
function timer(status) {
if(status == true)
timerDisplay.style.display = "block";
else if(status == false) {
timerDisplay.style.display = "none";
} else {
console.log("timer function called with unexpected parameter!");
}
}
function start(status) {
if(status == true)
startButton.style.display = "inline";
else if(status == false) {
startButton.style.display = "none";
} else {
console.log("start function called with unexpected parameter!");
}
}
function pause(status) {
if(status == true)
pauseButton.style.display = "inline";
else if(status == false) {
pauseButton.style.display = "none";
} else {
console.log("pause function called with unexpected parameter!");
}
}
function resume(status) {
if(status == true)
resumeButton.style.display = "inline";
else if(status == false) {
resumeButton.style.display = "none";
} else {
console.log("resume function called with unexpected parameter!");
}
}
function reset(status) {
if(status == true)
resetButton.style.display = "inline";
else if(status == false) {
resetButton.style.display = "none";
} else {
console.log("reset function called with unexpected parameter!");
}
}
function minput(status) {
if(status == true)
minutesInput.style.display = "inline";
else if(status == false) {
minutesInput.style.display = "none";
} else {
console.log("minput function called with unexpected parameter!");
}
}
timer(false);
start(true);
pause(false);
resume(false);
reset(false);
minput(true);
function startTimer(duration) {
let timer = duration * 60 + remainingTime;
let minutes, seconds;
timerInterval = setInterval(function () {
minutes = Math.floor(timer / 60);
seconds = timer % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
timerDisplay.textContent = minutes + ':' + seconds;
if (--timer < 0) {
clearInterval(timerInterval);
timerDisplay.textContent = "Time is up!";
sendNotification("Time is up!", "The timer you set has expired!");
start(false);
pause(false);
resume(false);
reset(true);
}
}, 1000);
}
function handleEnterKeyPress(event) {
if (event.key === 'Enter') {
event.preventDefault();
startButton.click();
}
}
minutesInput.addEventListener('keypress', handleEnterKeyPress);
startButton.addEventListener('click', function () {
let minutes = parseInt(minutesInput.value);
if(Number.isInteger(minutes) == true && minutes > 0) {
startTimer(minutes);
timer(true);
pause(true);
reset(true);
start(false);
pause(true);
minput(false);
minutesInput.value = '';
} else {
resetButton.click();
}
});
pauseButton.addEventListener('click', function () {
clearInterval(timerInterval);
remainingTime = parseInt(timerDisplay.textContent.split(':')[0]) * 60 + parseInt(timerDisplay.textContent.split(':')[1]);
resume(true);
pause(false);
});
resumeButton.addEventListener('click', function () {
startTimer(0);
resume(false);
pause(true);
});
resetButton.addEventListener('click', function() {
clearInterval(timerInterval);
timerDisplay.textContent = '00:00';
minutesInput.value = '';
remainingTime = 0;
timer(false);
start(true);
pause(false);
resume(false);
reset(false);
minput(true);
});