created stop watch
This commit is contained in:
commit
ec677f3b99
16
d.html
Normal file
16
d.html
Normal 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
33
d.js
Normal 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()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user