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.
84 lines (66 sloc)
1.88 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
<!doctype html> | |
<html class="no-js" lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Stopwatch App</title> | |
<link rel="stylesheet" href="css/style.css"> | |
<link rel="stylesheet" href="https://use.typekit.net/xpy5krv.css"> | |
<meta name="description" content="Stopwatch app that allows you to time anything whenever you want!"> | |
</head> | |
<body> | |
<div class="main"> | |
<h3>Online Stopwatch</h3> | |
<h1>00:00</h1> | |
<div class="buttons"> | |
<button class="start">Start</button> | |
<button class="reset">Reset</button> | |
</div> | |
</div> | |
<script> | |
let timer; | |
let seconds = 0; | |
let minutes = 0; | |
let btn = document.querySelector('button.start'); | |
let resetbtn = document.querySelector('button.reset'); | |
btn.addEventListener('click', function() { | |
if (btn.innerText === "Start") { | |
timer = setInterval(updateStopwatch, 1000); | |
btn.innerText = "Stop"; | |
} else { | |
clearInterval(timer); | |
btn.innerText = "Start"; | |
} | |
}) | |
resetbtn.addEventListener('click', function() { | |
clearInterval(timer); | |
seconds = 0; | |
minutes = 0; | |
displayTime(); | |
if (btn.innerText === "Stop") { | |
btn.innerText = "Start"; | |
} | |
}) | |
function updateStopwatch() { | |
seconds++; | |
if (seconds === 60) { | |
seconds = 0; | |
minutes++; | |
} | |
displayTime(); | |
} | |
function displayTime() { | |
const display = document.querySelector('h1'); | |
const formattedTime = formatTime(minutes) + ':' + formatTime(seconds); | |
display.textContent = formattedTime; | |
} | |
function formatTime(time) { | |
if (time < 10) { | |
return '0' + time; | |
} else { | |
return time; | |
} | |
} | |
</script> | |
</body> | |
</html> |