-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.js
87 lines (62 loc) · 2.17 KB
/
stop.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
const mainmenu = document.querySelector(".mainmenu");
const closemenu = document.querySelector(".closemenu");
const openmenu = document.querySelector(".openmenu");
openmenu.addEventListener("click", show);
closemenu.addEventListener("click", close);
function show() {
mainmenu.style.display = 'flex';
mainmenu.style.top = '0';
}
function close() {
mainmenu.style.top = '-100%';
}
function timeToString(time) {
let diffInHrs = time / 3600000;
let hh = Math.floor(diffInHrs);
let diffInMin = (diffInHrs - hh) * 60;
let mm = Math.floor(diffInMin);
let diffInSec = (diffInMin - mm) * 60;
let ss = Math.floor(diffInSec);
let diffInMs = (diffInSec - ss) * 100;
let ms = Math.floor(diffInMs);
let formattedMM = mm.toString().padStart(2, "0");
let formattedSS = ss.toString().padStart(2, "0");
let formattedMS = ms.toString().padStart(2, "0");
return `${formattedMM}:${formattedSS}:${formattedMS}`;
}
let startTime;
let elapsedTime = 0;
let timerInterval;
function print(txt) {
document.getElementById("display").innerHTML = txt;
}
function start() {
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(function printTime() {
elapsedTime = Date.now() - startTime;
print(timeToString(elapsedTime));
}, 10);
showButton("PAUSE");
}
function pause() {
clearInterval(timerInterval);
showButton("PLAY");
}
function reset() {
clearInterval(timerInterval);
print("00:00:00");
elapsedTime = 0;
showButton("PLAY");
}
function showButton(buttonKey) {
const buttonToShow = buttonKey === "PLAY" ? playButton : pauseButton;
const buttonToHide = buttonKey === "PLAY" ? pauseButton : playButton;
buttonToShow.style.display = "block";
buttonToHide.style.display = "none";
}
let playButton = document.getElementById("playButton");
let pauseButton = document.getElementById("pauseButton");
let resetButton = document.getElementById("resetButton");
playButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);
resetButton.addEventListener("click", reset);