Skip to content
Permalink
346fdc5664
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
84 lines (66 sloc) 1.88 KB
<!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>