-
Notifications
You must be signed in to change notification settings - Fork 0
/
myWatch.js
137 lines (119 loc) · 3.24 KB
/
myWatch.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
//Alarm Section starts from here
var alarmSound = new Audio();
alarmSound.src = "alarmSounds.mp3";
var alarmTimer;
function showTime() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
var date = d.toLocaleDateString();
var time = h + ":"+ m + ":"+ s;
document.getElementById("date").innerText = date;
document.getElementById("date").textContent = date;
document.getElementById("time").innerText = time;
document.getElementById("time").textContent = time;
setTimeout(showTime, 1000);
}
showTime();
function setAlarm(button) {
var ms = document.getElementById('alarmTime').valueAsNumber;
if(isNaN(ms)) {
alert('Invalid Date & Time');
return;
}
var alarm = new Date(ms);
var alarmTime = new Date(alarm.getUTCFullYear(), alarm.getUTCMonth(), alarm.getUTCDate(), alarm.getUTCHours(), alarm.getUTCMinutes(), alarm.getUTCSeconds());
var differenceInMs = alarmTime.getTime() - (new Date()).getTime();
if(differenceInMs < 0) {
alert('Specified time is already passed');
return;
}
alarmTimer = setTimeout(initAlarm, differenceInMs);
button.innerText = 'Cancel Alarm';
button.setAttribute('onclick', 'cancelAlarm(this);');
};
function cancelAlarm(button){
clearTimeout(alarmTimer);
button.innerText = "Set Alarm";
button.setAttribute('onclick', 'setAlarm(this);');
alarmSound.pause();
document.getElementById("alarmOptions").style.display = "none";
};
function initAlarm(){
alarmSound.play();
document.getElementById("alarmOptions").style.display = '';
};
function stopAlarm(){
alarmSound.pause();
alarmSound.currentTime = 0;
document.getElementById("alarmOptions").style.display = "none";
cancelAlarm(document.getElementById("alarmButton"));
};
function snooze(){
stopAlarm();
alarmTimer = setTimeout(initAlarm, 300000);
}
//Stop Watch Section starts from here
var hours= 0;
var minutes= 0;
var seconds= 0;
var displaySeconds = 0;
var displayMinutes = 0;
var displayHours = 0;
var interval = null;
var status = "stopped";
function stopWatchFunc(){
seconds++;
if (seconds / 60 ===1) {
seconds = 0;
minutes++;
if (minutes /60 === 1) {
minutes = 0;
hours++
}
}
if(seconds < 10){
displaySeconds = "0" + seconds.toString();
}
else{
displaySeconds = seconds;
}
if(minutes < 10){
displayMinutes = "0" + minutes.toString();
}
else{
displayMinutes = minutes;
}
if(hours < 10){
displayHours = "0" + hours.toString();
}
else{
displayHours = hours;
}
var display = document.getElementById("stopWatchDisplay");
display.innerHTML = displayHours + ":" + displayMinutes + ":" + displaySeconds;
}
function startStop(){
if(status === "stopped"){
interval = window.setInterval(stopWatchFunc, 1000);
document.getElementById("startBtn").innerHTML = "Stop";
status = "started";
}
else{
window.clearInterval(interval);
document.getElementById("startBtn").innerHTML = "Start";
status = "stopped";
}
}
function reset(){
window.clearInterval(interval);
seconds = 0;
minutes = 0;
hours = 0;
document.getElementById("stopWatchDisplay").innerHTML = "00:00:00";
document.getElementById("startBtn").innerHTML = "Start";
}