How To Make A Stopwatch In Javascript?

Making a stopwatch in Javascript is a great way to improve your skills in the language. This tutorial will show you how to make a stopwatch using Javascript.

Checkout this video:


Why would you want to make a stopwatch in JavaScript? Well, maybe you’re making a game where timing is important, or you want to analyze how long something takes. Building one from scratch is actually pretty simple. Let’s see how it’s done!

What you will need

-1x HTML file
-1x CSS file
-1x JavaScript file

In this tutorial, we will be making a stopwatch using HTML, CSS, and JavaScript.

We will start by creating the necessary files and folders. In your project’s root folder, create an “index.html” file and a “js” folder. index.html will hold our HTML markup and js will hold our JavaScript code. Create a “css” folder as well and an “index.css” file inside of it. This is where we will put our CSS code.

Next, we need to include the JavaScript file in our HTML document. Add the following line of code to the head element of your index.html file:

Your directory structure should now look like this:

– project_root/
– index.html
– css/
– index.css < – empty for now! - js/ < – empty for now!

Stopwatch design

A stopwatch is a handheld timepiece intended to measure the amount of time elapsed from a particular time and to record this interval. A large digital display shows the elapsed time. Buttons are used to start and stop the timing, and to reset the countdown to zero.

There are many ways to design a stopwatch in JavaScript. In this article, we will show you how to make a simple stopwatch using HTML, CSS, and JavaScript.

We will start by creating a container element with an id of “stopwatch”. Inside this container, we will create three child elements:

– A display element with an id of “display” that will show the elapsed time.
– A button with an id of “start” that will start the stopwatch.
– A button with an id of “stop” that will stop the stopwatch.
– A button with an id of “reset” that will reset the stopwatch.

Then we will style these elements using CSS. And finally, we will write some JavaScript code to create the stopwatch functionality.

Coding the stopwatch

In this section, we’ll go over the basic steps for coding a stopwatch in JavaScript. We’ll create a function to start the stopwatch, a function to stop the stopwatch, and a function to reset the stopwatch. We’ll also keep track of the time in milliseconds so that we can accurately display it on the screen.

To get started, open up your text editor and type the following:

function startStopwatch() {
// code to start the stopwatch goes here

function stopStopwatch() {
// code to stop the stopwatch goes here

function resetStopwatch() {
// code to reset the stopwatch goes here

Testing the stopwatch

To test the stopwatch, we’ll need to do the following:

1. Set up a test file with the HTML and CSS for our stopwatch.
2. Include the stopwatch.js file in our test file.
3. Write a test that checks that the stopwatch is initially stopped when the page loads.
4. Write a test that starts the stopwatch when the start button is clicked, and pauses it when the pause button is clicked.
5. Write a test that reset the stopwatch when the reset button is clicked.
6. Run our tests and make sure they all pass!

Adding CSS styles

In order to add CSS styles to our stopwatch, we need to first create a CSS file and link it to our HTML file. In your text editor, create a new file and save it as “stopwatch.css” in the same directory as your HTML file. Then, add the following code to your CSS file:

* {
margin: 0;
padding: 0;
box-sizing: border-box;

body {
font-family: sans-serif;

#stopwatch {
width: 50%;
max-width: 500px;
margin: 100px auto;

Adding finishing touches

Now that we have our basic stopwatch functionality in place, let’s add some finishing touches. We’ll start by giving our stopwatch a more user-friendly interface.


We hope you enjoyed this tutorial on how to make a stopwatch in Javascript. If you have any questions or comments, please feel free to leave them below.

Scroll to Top