-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.84 KB
/
index.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
let startTime, updatedTime, difference, tInterval, running = false;
function startStopwatch() {
if (!running) {
startTime = new Date().getTime() - (difference || 0);
tInterval = setInterval(getShowTime, 10);
running = true;
}
}
function stopStopwatch() {
if (running) {
clearInterval(tInterval);
difference = new Date().getTime() - startTime;
running = false;
}
}
function resetStopwatch() {
clearInterval(tInterval);
running = false;
difference = 0;
document.getElementById('hours').innerHTML = '00';
document.getElementById('minutes').innerHTML = '00';
document.getElementById('seconds').innerHTML = '00';
document.getElementById('milliseconds').innerHTML = '00';
}
function getShowTime() {
updatedTime = new Date().getTime();
difference = updatedTime - startTime;
let milliseconds = Math.floor((difference % 1000) / 10);
let seconds = Math.floor((difference / 1000) % 60);
let minutes = Math.floor((difference / (1000 * 60)) % 60);
let hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
milliseconds = (milliseconds < 10) ? "0" + milliseconds : milliseconds;
seconds = (seconds < 10) ? "0" + seconds : seconds;
minutes = (minutes < 10) ? "0" + minutes : minutes;
hours = (hours < 10) ? "0" + hours : hours;
document.getElementById('hours').innerHTML = hours;
document.getElementById('minutes').innerHTML = minutes;
document.getElementById('seconds').innerHTML = seconds;
document.getElementById('milliseconds').innerHTML = milliseconds;
}
document.getElementById('start').addEventListener("click", startStopwatch);
document.getElementById('stop').addEventListener("click", stopStopwatch);
document.getElementById('reset').addEventListener("click", resetStopwatch);