created stop watch

This commit is contained in:
Jonathan Branan 2024-09-02 11:52:00 -05:00
commit ec677f3b99
2 changed files with 49 additions and 0 deletions

16
d.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stop Watch</title>
</head>
<body>
<h3>Stop Watch</h3>
<h1 id="time"> 00:00</h1>
<button onclick="startClock()">Start</button>
<button onclick="stopClock()">Stop</button>
<button onclick="resetClock()">Reset</button>
<script src="d.js"></script>
</body>
</html>

33
d.js Normal file
View File

@ -0,0 +1,33 @@
let secondsElapsed = 0
let interval = null
const time = document.getElementById("time")
function padStart(value) {
return String(value).padStart(2, "0")
}
function setTime() {
const minutes = Math.floor(secondsElapsed / 60)
const seconds = secondsElapsed % 60
time.innerHTML = `${padStart(minutes)}:${padStart(seconds)}`
}
function timer(){
secondsElapsed ++
setTime()
}
function startClock() {
if (interval) resetClock()
interval = setInterval(timer, 1000)
}
function stopClock() {
clearInterval(interval)
}
function resetClock() {
stopClock()
secondsElapsed = 0
setTime()
}