-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript1.js
104 lines (76 loc) · 2.75 KB
/
script1.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
function navSlide() {
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");
burger.addEventListener("click", () => {
//Toggle Nav
nav.classList.toggle("nav-active");
//Animate Links
navLinks.forEach((link, index) => {
if (link.style.animation) {
link.style.animation = ""
} else {
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`;
}
});
//Burger Animation
burger.classList.toggle("toggle");
});
}
navSlide();
// Convert time to a format of hours, minutes, seconds, and milliseconds
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}`;
}
// Declare variables to use in our functions below
let startTime;
let elapsedTime = 0;
let timerInterval;
// Create function to modify innerHTML
function print(txt) {
document.getElementById("display").innerHTML = txt;
}
// Create "start", "pause" and "reset" functions
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");
}
// Create function to display buttons
function showButton(buttonKey) {
const buttonToShow = buttonKey === "PLAY" ? playButton : pauseButton;
const buttonToHide = buttonKey === "PLAY" ? pauseButton : playButton;
buttonToShow.style.display = "block";
buttonToHide.style.display = "none";
}
// Create event listeners
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);