-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (129 loc) · 3.83 KB
/
app.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
// Format the hours, minutes and seconds as 00:00:00
function formatTime(num) {
if (num < 10) {
num = "0" + num;
}
return num;
}
// Display current time in timer view
function startTime() {
const today = new Date();
let hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
hour = formatTime(hour);
minute = formatTime(minute);
second = formatTime(second);
document.querySelector("#current-time").innerHTML = hour + ":" + minute + ":" + second;
setTimeout(function () {
startTime();
}, 500);
}
// Set event listener to start button, to start running input time
let startButton = document.querySelector("#start-button");
let input = document.querySelector("#number");
startButton.addEventListener("click", function (e) {
e.preventDefault();
startButton.disabled = false;
if (input.value === "") {
alert("Please enter the number of minutes");
} else {
runTime();
startButton.disabled = true;
}
});
startButton.addEventListener("click", function (e) {
e.preventDefault();
timerTab();
});
// Counting the time from 0 up to the input time
function runTime() {
let runSeconds = 0;
let countDown = setInterval(() => {
if (runSeconds < input.value * 60) {
++runSeconds;
let totalSeconds = runSeconds % 60;
let totalHours = Math.floor(runSeconds / 3600);
let totalMinutes = Math.floor(runSeconds / 60 - totalHours * 60);
totalHours = formatTime(totalHours);
totalMinutes = formatTime(totalMinutes);
totalSeconds = formatTime(totalSeconds);
// Displaying the counter in the timer view
document.querySelector("#hours-passed").innerHTML = totalHours;
document.querySelector("#minutes-passed").innerHTML = totalMinutes;
document.querySelector("#seconds-passed").innerHTML = totalSeconds;
document.title = totalHours + ":" + totalMinutes + ":" + totalSeconds;
}
// When input time is reached, clear counter and play chimes sound
if (runSeconds === input.value * 60) {
clearInterval(countDown);
play();
input.value = "";
}
}, 1000);
// Play windchimes and show alert message
function play() {
let alertSound;
alertSound = new Audio("./audio/windchimes.wav");
alertSound.play();
setTimeout(function () {
alert("The time has elapsed");
}, 500);
setTimeout(function () {
alert("Icon made by Flat Icons (www.flaticon.com). Font: open source Recurso Sans and GNU Unifont (Roman Czyborra and Paul Hardy).")
}, 1000)
}
}
// Reset button to clear input field
document.querySelector("#reset-button").addEventListener("click", function (e) {
clearInterval(countDown);
document.querySelector("#number").value = "";
document.querySelector("#hours-passed").innerHTML = formatTime(0);
document.querySelector("#minutes-passed").innerHTML = formatTime(0);
document.querySelector("#seconds-passed").innerHTML = formatTime(0);
e.preventDefault();
});
// Add button with event listener, plus function to change background color
let body = document.querySelector(".timer-body");
let toggleButton = false;
let colorButton = document.querySelector(".color-button");
colorButton.addEventListener("click", function (e) {
changeBackground();
e.preventDefault();
});
function changeBackground() {
const colors = [
"#aed6f1",
"#76d7c4",
"#f7dc6f",
"#f8c471",
"#e74c3c",
"#f1c40f",
"#c39bd3",
"#a569bd",
"#45b39d",
"#28b463",
"#eb984e",
"#27ae60",
"#05E3EE",
"#3498db",
"#dc7633",
"#f1c40f",
"#0ad4d4",
"#5811a4",
"#9437f7",
"#d81b06",
"#fb311a",
"#74fb1a",
"#e4fb1a",
"#1a55fb",
"#25418f",
"#05098a",
"#1212e0",
"#f41005",
"#FFC300",
];
let newColor = colors[Math.floor(Math.random() * colors.length)];
body.style.background = newColor;
}
startTime();