How to Create a Stopwatch Using HTML, CSS, and JavaScript

author-profile-image

Marketing Team

Thursday 15th June, 2023

clock3 Mins Read

a graphic of a pie chart and a computer

In this tutorial, we will learn how to create a stopwatch using HTML, CSS, and JavaScript. A stopwatch is a useful tool for measuring time intervals accurately. By following the steps outlined below, you will be able to build a functional stopwatch that can be used in various applications. Let's dive right in!


Setting up the HTML Structure

To begin, we need to 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, we have a container div with the class "stopwatch" that contains a heading, a display area to show the time, and three buttons for start, stop, and reset actions.


Styling the Stopwatch with CSS

Next, let's 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;
}

In the CSS code above, we apply some basic styling to the stopwatch elements. Feel free to customize the styles to match your preferences.


Implementing Stopwatch Functionality with JavaScript

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);

In the JavaScript code above, we define functions for starting, stopping, and resetting the stopwatch. The startTimer function uses the setInterval method to update the display every 10 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.

a yellow circle with JS black letters

Conclusion

Congratulations! You have successfully created a stopwatch using HTML, CSS, and JavaScript. You can now 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!

final thought

a grey symbol with curved linesRemember, mastering HTML, CSS, and JavaScript opens up a world of opportunities in web development. By creating projects like this stopwatch, you can gain valuable experience and improve your skills. Keep learning and building, and you'll continue to grow as a developer. a grey symbol with curved lines

by Marketing Team

Get in touch

Our team will reach you out soon !!

Scale
your team
faster with
our pre-vetted
remote
Talent.

Hire now  

final thought

a grey symbol with curved linesRemember, mastering HTML, CSS, and JavaScript opens up a world of opportunities in web development. By creating projects like this stopwatch, you can gain valuable experience and improve your skills. Keep learning and building, and you'll continue to grow as a developer. a grey symbol with curved lines

by Marketing Team

Related Blogs

Facebook icon Instagram icon Twitter icon Linkedin icon