Simple Stopwatch using JavaScript & CSS
Tags: html, javascript, webdev, beginners
Published: 2022-01-13 17:36:52

In this article you will learn how to make a simple stopwatch using JavaScript and CSS. Earlier I shared with you a tutorial on [Simple JavaScript StopWatch](https://www.foolishdeveloper.com/2021/10/simple-stopwatch-using-javascript.html). 

First I created a box at the top of the webpage. Then I made three small boxes which can be seen in the times. At the end there are two buttons that will control the stopwatch.

## Simple Stopwatch using JavaScript

The design shown in this tutorial is absolutely simple. There are no restart buttons but only start and stop buttons. 

[**Watch its live demo**](https://www.foolishdeveloper.com/2021/11/stopwatch-using-javascript.html) to learn how it works. Here I have tried to share step-by-step tutorial. For this you need to have a basic idea about HTML CSS and JavaScript.

### 1. Make a box on the webpage

I created a box using the following HTML and CSS codes. The width of this box: 300px, `height: 90px` and background color white has been used.
```html
<div id="stopwatch">
 
</div>
```
```css
body {
  font-family: arial;
  background: #0776de;
}

#stopwatch {
  width: 300px;
  height: 90px;
  margin: 100px auto;
  background: white;
  color: #333;
  border-radius: 10px;
  padding: 60px 50px 100px;
  text-align: center;
  font-size: 30px;
}

```

![Make a box on the webpage](https://blogger.googleusercontent.com/img/a/AVvXsEhbHiubmkijYYnSHw6orn2q_aLPhEJTFITZ9say2V-Ow-k-ud8eA1siD4job69EGrRDDuPpLhcNrfk5J_aD5RVlbwySfa1pgPb9euv7IfoIYw2PH293mVipSHzbXnuWORjirpHwSv1JLCMsF4g45vFkIHiKxQuTz0xRQPYZJ2flFmI95h8ZpSe9YkoD7A=s16000)
### 2. Create a count view display
Now a display has been created in which the times can be seen. There will be countdown in minutes, seconds and milliseconds. I made three small boxes for the times. These boxes depend on `width: 55px` and height padding.
```html
<div class="time">
 <span id="minute">00</span>
 <span id="second">00</span>
 <span id="ms">00</span>
</div>
```
```css
.time{
  margin-left: -25px;
} 

#stopwatch span {
  background: #c7e0f8;
  color: #0776de;
  padding: 10px;
  width: 55px;
  border-radius: 5px;
  margin: 3px;
  display: inline-block;
  margin-left: 20px;
}
```
![Create a count view display](https://blogger.googleusercontent.com/img/a/AVvXsEjV5Mww6LGN3hqJANftTWWiYp4A1Qf2TYGaz2sb1HCdxKT5FV1LEcuqe5Tjw_Mhbcy_0JKmwwi_hAvUDE64KZGaZVIEfk-NyyMHoizmkWJeW-7MG8ilgDrm8_IogfOo2khPryhem0tc6pAp43XKSdlwVIfj_4wnpRawCZjjsj2ZR-R9woPK399gdJdHMA=s16000)
### 3. Stop and Start buttons of JavaScript Stopwatch
Now two buttons have been created which will control this Simple Stopwatch. The first button is to start the count and the second button is to turn off the count. However, there is no restart button. 

I have previously designed a [simple JavaScript Stopwatch](https://www.foolishdeveloper.com/2021/10/simple-stopwatch-using-javascript.html) with a restart button. You can watch that tutorial. `width: 140px`, `height: 50px` used. I have used different background-colors for the two buttons.

In addition, hover effect has been used in each button. The color of the background will change when you click on the buttons or move the mouse.
```html
  <button id="start" onclick="start();">Start</button>
  <button id="stop" onclick="stop();">Stop</button>
```
```css
#stopwatch button {
  font-size: 22px;
  -webkit-appearance: none;
  border: none;
   background-color: #2e80b3;
  color: white;
  border-radius: 5px;
  width: 140px;
  height: 50px;
  transition: .3s;
}
#stopwatch button:first-of-type {
  margin-top: 15px;
}

#stopwatch button:last-child{
    background-color: #ae7617;
}

#stopwatch button:hover {
  background: #333;
  color: #fff;
  cursor: pointer;
}
```
![Simple Stopwatch using JavaScript](https://blogger.googleusercontent.com/img/a/AVvXsEj-YgR1ZOMDFHf4T6wm88YEX-VMgDSz_tjhSGCy8Pvgl7VtoKMbVBtclk7N-fa5r0H_64Bskbe16mWsLaFY-XI_Fb9sqlPyS9_Rx1s9SzM-yp7o8iIMn6BELPSnRDPJVXDt90Son8Tuv0cY6amwUZ3_nI7GhH-3DGGWht3SqJ-mTo9yLCv7yiAXPQuCKQ=s16000)

### 4. Activate Simple Stopwatch using JavaScript
Now is the time to implement this Simple Stopwatch using JavaScript. If you know basic JavaScript you can easily understand the following code. Here I have given the necessary explanation of each line.
```javascript
//The current count of the timer is zero
var timer = 0;
var timerInterval;
//One of the id functions of the three time boxes has been assigned a global constant.
var ms = document.getElementById('ms');
var second = document.getElementById('second');
var minute = document.getElementById('minute');

function start() {
  stop();
//All calculations are stored in "timerInterval".
//Here "setInterval" is used and 1000/60 seconds is set.
//As a result, this calculation will be updated every 1 millisecond
  timerInterval = setInterval(function() {
//Now the value of 'timer' has been set
//We know x + = y => x = x + y
    timer += 1/60;
//Now the value of 'milliseconds' has been set. It is also stored in "msVal".
    msVal = Math.floor((timer - Math.floor(timer))*100);
//The value of 'seconds' has been determined in the same way.
    secondVal = Math.floor(timer) - Math.floor(timer/60) * 60;
//The value of 'minute' has been determined.
    minuteVal = Math.floor(timer/60);

//With the help of 'innerHTML' all the information is displayed in the webpage
//The condition here is that when the value of time is less than 10 then a 0 will be added before that time.
// For milliseconds
ms.innerHTML = msVal < 10 ? "0" + msVal.toString() : msVal;
//For seconds
second.innerHTML = secondVal < 10 ? "0" + secondVal.toString() : secondVal;
// For Minute
minute.innerHTML = minuteVal < 10 ? "0" + minuteVal.toString() : minuteVal;

  }, 1000/60);
}
//Now you have to activate the stop button
//The clearInterval() function in javascript clears the interval which has been set by setInterval() function before that.
function stop() {
  clearInterval(timerInterval);
}
```
![Simple Stopwatch using JavaScript](https://blogger.googleusercontent.com/img/a/AVvXsEioeKMF1bQufYFz36IBWZMGRM0ouCvzdqdKf7Qz6JHNWJP7KGTvMqOUrEL4eElvjWM5sypaXzCDU9A6VK13YRVPDyFmLCNtLDNTKDJw1HGpdpUTYKJBorfVAFaRpmRarYIv6ocflW7HFJePynU8mgxoS5WoGp8aItm_Rsx3JY-SRwErFHgcu1YcUDsJkA=s16000)

Hopefully the above code and tutorial has helped you to know how Simple Stopwatch was created using HTML CSS JavaScript.

**Related Post:**
1. [Simple Weather App using JavaScript](https://www.foolishdeveloper.com/2021/12/simple-weather-app-using-javascript.html) 
2. [Make a Todo List using JavaScript](https://www.foolishdeveloper.com/2021/11/todo-list-javascript.html)
3. [Simple Stopwatch using JavaScript](https://www.foolishdeveloper.com/2021/10/simple-stopwatch-using-javascript.html)
4. [Skeleton Screen Loading Animation](https://www.foolishdeveloper.com/2021/09/skeleton-screen-loading-animation.html)
5. [Javascript Age Calculator](https://www.foolishdeveloper.com/2021/07/javascript-age-calculator.html)
6. [Random Password Generator with JavaScript](https://www.foolishdeveloper.com/2021/07/random-password-generator-javascript.html)
7. [Automatic Image Slider in Html, CSS](https://www.foolishdeveloper.com/2021/07/automatic-image-slider.html) 
8. [Sidebar Menu Using HTML CSS](https://www.foolishdeveloper.com/2021/06/sidebar-menu-using-html-css-javascript.html) 

If you want to download the [required source code](https://www.foolishdeveloper.com/2021/11/stopwatch-using-javascript.html) of this Stopwatch then you can use this link. If you have any problems, you can comment.

You can visit my blog for more tutorials like this. 😊
https://www.foolishdeveloper.com/