In this tutorial, you will learn how to create a stopwatch using HTML, CSS, and JavaScript. A stopwatch is a valuable tool to measure time intervals accurately. Following the steps outlined below, you can build a functional stopwatch for various applications. Let's dive right in!
To begin, you must create the basic HTML structure for our stopwatch. Here's an example of the HTML code:
Html code
<div class="stopwatch">
<h1>Stopwatch</h1>
<div class="display">00:00:00</div>
<button class="start">Start</button>
<button class="stop">Stop</button>
<button class="reset">Reset</button>
</div>
In the code above, you have a container div with the " stopwatch " class containing a heading, a display area to show the time, and three buttons for start, stop, and reset actions.
Next, add some CSS styles to make our stopwatch visually appealing. Here's an example of the CSS code:
css code
.stopwatch {
text-align: center;
margin-top: 50px;
}
.display {
font-size: 36px;
font-weight: bold;
margin-bottom: 20px;
}
button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: white;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
button:active {
background-color: #3e8e41;
}
Apply basic styling to the stopwatch elements in the CSS code above. Feel free to customize the styles to match your preferences.
Now, let's add JavaScript code to make our stopwatch functional. We'll use the Date object to track the elapsed time. Here's an example of the JavaScript code:
// Javascript code
var startTime, elapsedTime, timerInterval;
function startTimer() {
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(function () {
var currentTime = Date.now();
elapsedTime = currentTime - startTime;
updateDisplay(elapsedTime);
}, 10);
}
function stopTimer() {
clearInterval(timerInterval);
}
function resetTimer() {
clearInterval(timerInterval);
elapsedTime = 0;
updateDisplay(elapsedTime);
}
function updateDisplay(time) {
var milliseconds = Math.floor((time % 1000) / 10);
var seconds = Math.floor((time / 1000) % 60);
var minutes = Math.floor((time / 1000 / 60) % 60);
var hours = Math.floor((time / 1000 / 60 / 60) % 24);
var displayTime = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0') + '.' +
milliseconds.toString().padStart(2, '0');
document.querySelector('.display').textContent = displayTime;
}
document.querySelector('.start').addEventListener('click', startTimer);
document.querySelector('.stop').addEventListener('click', stopTimer);
document.querySelector('.reset').addEventListener('click', resetTimer);
The JavaScript code above defines functions for starting, stopping, and resetting the stopwatch. The startTimer function uses the setInterval method to update the display every ten milliseconds, while the stopTimer function clears the interval. The resetTimer function resets the elapsed time and updates the display accordingly. The updateDisplay function formats the time into hours, minutes, seconds, and milliseconds and updates the display element.
You created a stopwatch using HTML, CSS, and JavaScript. Now, you can incorporate this stopwatch into your web applications and enhance their time measurement capabilities. Feel free to customize the design and functionality to suit your specific requirements. Keep practicing and exploring more possibilities with web development!
If you enjoyed this piece, we've crafted a related article delving into The Distinction between JavaScript and Java. Explore it here.