Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
2024-spring-dmd-3475/week-10/index.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (43 sloc)
1.18 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Making stopwatch | |
<section> | |
<p1>0m</p1> | |
<p2>0s</p2> | |
<button id="start">Start</button> | |
<button id="reset">Reset</button> | |
</section> | |
<script> | |
let start = document.querySelector("#start") | |
let stop = document.querySelector("#stop") | |
let restart = document.querySelector("#restart") | |
let p1 = document.querySelector("p1") | |
let p2 = document.querySelector("p2") | |
let seconds = 0; | |
let minutes = 0; | |
let timer; | |
function startTimer(){ | |
timer = setInterval(updateDisplay, 1000) | |
updateDisplay() | |
}; | |
function updateDisplay(){ | |
if (seconds == 59){ | |
minutes = minutes + 1; | |
seconds = -1; | |
} | |
seconds++; | |
p1.innerHTML = `${minutes}m` | |
p2.innerHTML = `${seconds}s` | |
console.log("interval running", seconds) | |
} | |
function stopTimer(){ | |
clearInterval(timer) | |
} | |
function resetTimer(){ | |
stopTimer() | |
console.log("reset") | |
seconds = -1; | |
minutes = 0; | |
updateDisplay() | |
} | |
start.addEventListener('click', startTimer) | |
reset.addEventListener('click', resetTimer) | |
</script> | |