-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (71 loc) · 2 KB
/
script.js
File metadata and controls
85 lines (71 loc) · 2 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
var start = document.getElementById('start');
var reset = document.getElementById('reset');
var pause = document.getElementById('pause');
var h = document.getElementById("hour");
var m = document.getElementById("minute");
var s = document.getElementById("sec");
//store a reference to the startTimer variable
var startTimer = null;
start.disabled = true;
[h, m, s].forEach(input => {
input.addEventListener('input', () => {
if (h.value > 0 || m.value > 0 || s.value > 0) {
start.disabled = false;
} else {
start.disabled = true;
}
});
});
start.addEventListener('click', function(){
//initialize the variable
function startInterval(){
startTimer = setInterval(function() {
timer();
}, 1000);
}
startInterval();
start.style.display = "none";
pause.style.display = "inline-block";
reset.style.display = "inline-block";
})
pause.addEventListener('click', function(){
stopInterval();
pause.style.display = "none";
start.style.display = "inline-block";
});
reset.addEventListener('click', function(){
h.value = 0;
m.value = 0;
s.value = 0;
//stop the timer after pressing "reset"
stopInterval();
start.disabled = true;
start.style.display = "inline-block";
pause.style.display = "none";
reset.style.display = "none";
})
function timer(){
if(h.value == 0 && m.value == 0 && s.value == 0){
h.value = 0;
m.value = 0;
s.value = 0;
start.disabled = true;
} else if(s.value != 0){
s.value--;
} else if(m.value != 0 && s.value == 0){
s.value = 59;
m.value--;
} else if(h.value != 0 && m.value == 0){
m.value = 60;
h.value--;
}
if (h.value == 0 && m.value == 0 && s.value == 0) {
start.disabled = true;
}
return;
}
//stop the function after pressing the reset button,
//so the time wont go down when selecting a new time after pressing reset
function stopInterval() {
clearInterval(startTimer);
}