-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
112 lines (98 loc) · 2.98 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
let timerObj = {
minutes: 0,
seconds: 0,
timerId: 0,
};
function soundAlarm() {
let amount = 3;
let audio = new Audio("13767_morning_alarm.mp3");
function playSound() {
audio.pause();
audio.currentTime = 0;
audio.play();
}
for (let i = 1; i < amount; i++) {
setTimeout(playSound, 1200 * i);
}
}
function updateValue(key, value) {
if (value < 0) {
value = 0;
console.log("Positive Numbers Only");
}
if (key == "seconds") {
if (value < 10) {
value = "0" + value;
}
if (value > 59) {
value = 59;
}
}
$("#" + key).html(value || 0);
timerObj[key] = value;
console.log("Min", timerObj.minutes);
console.log("Sec", timerObj.seconds);
}
(function detectChanges(key) {
console.log("Detect Changes");
let input = "#" + key + "-input";
$(input).change(function () {
updateValue(key, $(input).val());
});
$(input).keyup(function () {
updateValue(key, $(input).val());
});
return arguments.callee;
})("minutes")("seconds");
function startTimer() {
buttonManager(["start", false], ["pause", true], ["stop", true]);
freezeInputs();
timerObj.timerId = setInterval(function () {
timerObj.seconds--;
if (timerObj.seconds < 0) {
if (timerObj.minutes == 0) {
soundAlarm();
return stopTimer;
}
timerObj.seconds = 59;
timerObj.minutes--;
}
updateValue("minutes", timerObj.minutes);
updateValue("seconds", timerObj.seconds);
}, 1000);
}
function stopTimer() {
clearInterval(timerObj.timerId);
buttonManager(["start", true], ["pause", false], ["stop", false]);
unfreezeInputs();
updateValue("minutes", $("#minutes-input").val());
updateValue("seconds", $("#seconds-input").val());
// If the seconds is falsy, or undefined, set seconds to "0". Just 1 zero because line 29 checks if the value is less than 10, and if it is it will add an extra zero for you.
// The seconds will by default be undefined. Expliclty setting the seconds to 0 will prevent formats such as 1:0 or 2:0 when the timer expires, where the correct format should be 1:00 or 2:00.
let seconds = $("#seconds-input").val() || "0";
updateValue("seconds", seconds);
}
function pauseTimer() {
buttonManager(["start", true], ["pause", false], ["stop", true]);
clearInterval(timerObj.timerId);
}
["start", true], ["stop"];
//this is rest operator
function buttonManager(...buttonsArray) {
for (let i = 0; i < buttonsArray.length; i++) {
let button = "#" + buttonsArray[i][0] + "-button";
if (buttonsArray[i][1]) {
$(button).removeAttr("disabled");
} else {
$(button).attr("disabled", "disabled");
}
}
}
function freezeInputs() {
$("#minutes-input").attr("disabled", "disabled");
$("#seconds-input").attr("disabled", "disabled");
}
function unfreezeInputs() {
$("#minutes-input").removeAttr("disabled");
$("#seconds-input").removeAttr("disabled");
}