-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
69 lines (60 loc) · 1.88 KB
/
main.js
File metadata and controls
69 lines (60 loc) · 1.88 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
let a;
let date;
let time;
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
setInterval(() => {
a = new Date();
date = a.toLocaleDateString(undefined, options);
time = a.getHours() + ' : ' + a.getMinutes() + ' : ' + a.getSeconds();
document.getElementById('time').innerHTML = time + ' <br>on ' + date;
}, 1000);
var start = document.getElementById('start');
var reset = document.getElementById('reset');
var hours = document.getElementById("hour");
var minutes = document.getElementById("minute");
var seconds = document.getElementById("sec");
//store a reference to the startTimer variable
var startTimer = null;
start.addEventListener('click', function () {
//initialize the variable
function startInterval() {
startTimer = setInterval(function () {
if (hours.value == 0 && minutes.value == 0 && seconds.value == 0) {
alert("Time Over");
stopInterval();
return;
}
else {
timer();
}
}, 1000);
}
startInterval();
})
reset.addEventListener('click', function () {
hours.value = 0;
minutes.value = 0;
seconds.value = 0;
//stop the timer after pressing "reset"
stopInterval()
})
function timer() {
if (hours.value == 0 && minutes.value == 0 && seconds.value == 0) {
hours.value = 0;
minutes.value = 0;
seconds.value = 0;
// alert("Hello");
} else if (seconds.value != 0) {
seconds.value--;
} else if (minutes.value != 0 && seconds.value == 0) {
seconds.value = 59;
minutes.value--;
} else if (hours.value != 0 && minutes.value == 0) {
minutes.value = 60;
hours.value--;
}
return;
}
function stopInterval() {
clearInterval(startTimer);
}