Skip to content

Commit

Permalink
Stopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
nlz18001 committed Mar 27, 2024
1 parent 1857cee commit 361f54c
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions week-10/stopwatch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stopwatch</title>
</head>
<style>
h1 {
font-family: Arial, Helvetica, sans-serif;
display: flex;
justify-content: center;
margin-top: 10px;
}

#display {
font-family: Arial, Helvetica, sans-serif;
display: flex;
justify-content: center;
border: solid 1px black;
padding-top: 5px;
padding-bottom: 5px;
}

.button {
display: flex;
flex-direction: row;
justify-content: center;
gap: 10px;
margin-top: 10px;
margin-bottom: 10px;
}

.container {
border: solid 1px black;
max-width: fit-content;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
}

body {
display: flex;
justify-content: center;
}
</style>
<body>
<div class="container">
<h1>Stopwatch</h1>
<div id="display">0</div>
<div class="button">
<button id="startBtn">Start</button>
<button id="stopBtn">Stop</button>
<button id="resetBtn">Reset</button>
</div>
</div>
<script>
let timerInterval;
let elapsedTime = 0;
const display = document.getElementById('display');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const resetBtn = document.getElementById('resetBtn');

function startTimer() {
timerInterval = setInterval(updateDisplay, 1000);
startBtn.disabled = true;
stopBtn.disabled = false;
}

function stopTimer() {
clearInterval(timerInterval);
startBtn.disabled = false;
stopBtn.disabled = true;
}

function resetTimer() {
clearInterval(timerInterval);
elapsedTime = 0;
display.textContent = elapsedTime;
startBtn.disabled = false;
stopBtn.disabled = true;
}

function updateDisplay() {
elapsedTime++;
display.textContent = elapsedTime;
}

startBtn.addEventListener('click', startTimer);
stopBtn.addEventListener('click', stopTimer);
resetBtn.addEventListener('click', resetTimer);
</script>
</body>
</html>

0 comments on commit 361f54c

Please sign in to comment.