-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
181 lines (155 loc) · 3.87 KB
/
timer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var lefttimex=document.querySelector(".maindisplay");
var disptime=document.querySelector(".displaytime");
var button20sec=document.getElementById("20sec");
var button5min=document.getElementById("5min");
var button15min=document.getElementById("15min");
var button30min=document.getElementById("30min");
var audio=document.getElementById("player");
var resetx=document.getElementById("re");
var marker=0;
document.formx.addEventListener("submit",function(e)
{
e.preventDefault();
var sol;
sol=this.minutes.value;
console.log(sol);
timer(sol*60);
this.reset();
});
button15min.addEventListener("click",fun15m);
button5min.addEventListener("click",fun5m);
button30min.addEventListener("click",fun30m);
button20sec.addEventListener("click",fun20s);
resetx.addEventListener("click",resetxx);
var countdown;
function fun20s()
{
timer(20);
};
function fun5m()
{
timer(60*5);
};
function fun15m()
{
timer(15*60);
};
function fun30m()
{
timer(30*60);
};
console.log("attached");
//this is the main time counter and input is in seconds
//marker is here useless
function timer(seconds)
{
clearInterval(countdown);
//before we start a timer we must clear all existing timers
marker=1;
display(seconds);
var now=Date.now();
var then= now+seconds*1000;
marker=1;
console.log({now,then});
displayend(then);
countdown=setInterval(function(){
var lefttime;
lefttime=Math.round((then-Date.now())/1000);
if(lefttime<=0)
{
marker=0;
clearInterval(countdown);
timeend();
audio.play();
setTimeout(audiopp,9000);
}
display(lefttime);
},1000);
};
// function to display time
function display(seconds)
{
var minutes,remseconds,hours,zero;
hours=Math.floor(seconds/3600);
minutes=Math.floor(seconds/60);
minutes=minutes%60;
remseconds=seconds%60;
zero=0;
console.log({minutes,remseconds});
if(hours!=0)
{
if(remseconds>=10)
{
if(minutes>=10)
{
var dispp= `${hours}:${minutes}:${remseconds}`;
}
else
{
var dispp= `${hours}:${zero}${minutes}:${remseconds}`;
}
}
// var dispp= `${hours}:${minutes}:${remseconds}`;
else if(remseconds<10)
{
if(minutes>=10)
{
var dispp= `${hours}:${minutes}:${zero}${remseconds}`;
}
else
{
var dispp= `${hours}:${zero}${minutes}:${zero}${remseconds}`;
}
// var dispp= `${hours}:${minutes}:${zero}${remseconds}`;
}
}
else
{
if(remseconds>=10)
var dispp= `${minutes}:${remseconds}`;
else if(remseconds<10)
{
var dispp= `${minutes}:${zero}${remseconds}`;
}
}
if(remseconds===0&&minutes===0&&hours===0)
{
dispp=`TIME UP!!`;
}
document.title=dispp;
lefttimex.textContent=dispp;
};
function displayend(time)
{
var zero=0;
var end=new Date(time);
var hours=end.getHours();
var minutes=end.getMinutes();
if(minutes<10)
{
disptime.textContent=`Be back at ${hours}:${zero}${minutes}`;
}
else
disptime.textContent=`Be back at ${hours}:${minutes}`;
};
function timeend()
{
var diss;
diss=``;
disptime.textContent=diss;
};
//reset function
function resetxx()
{
clearInterval(countdown);
console.log("reset called");
lefttimex.textContent=`Set Time`;
disptime.textContent=`Return time`;
audio.pause();
var lol=`Timer`;
document.title=lol;
};
function audiopp()
{
audio.pause();
};