By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > How To Make a Stopwatch In JavaScript 2022
JavaScript

How To Make a Stopwatch In JavaScript 2022

Admin
Last updated: 2022/12/02 at 4:10 AM
Admin
Share
10 Min Read
How To Make a Stopwatch In JavaScript

In this article, we will learn How To Make a Stopwatch In Javascript. Of course, we will use some HTML, CSS but main role will be of JavaScript. This stopwatch will have three buttons and a timer, buttons will be for starting time, for pausing time, and last will be for restarting the timer. It is a very basic and beginner-friendly project so if you’re new to HTML, CSS, and JavaScript, that you also can make it very easily.

Contents
Pre-requisites To Make Stopwatch In JavaScriptCreating Basic Structure for StopwatchStyling and Animating The TimerStyling ButtonsAdding Functionalities To ButtonsFull Source Code of How To Make a Stopwatch In JavaScriptOutputYou may also like:

Pre-requisites To Make Stopwatch In JavaScript

  • Basic knowledge of HTML.
  • Basic knowledge of CSS.
  • Good knowledge of JavaScript.
View Demo

Creating Basic Structure for Stopwatch

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stopwatch</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <p>
                <span class="mins">00</span>:<span class="seconds">00</span>:<span class="tens">00</span>
            </p> <br>
            <button class="btn-start">Start</button>
            <button class="btn-stop">Stop</button>
            <button class="btn-reset">Reset</button>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
*{
    margin: 0;
    padding: 0;
    font-family: 'Source Sans Pro', sans-serif;
}
.container{
    background-color: #222242;
    height: 100vh;
    width: 100%;
    text-align: center;
    position: absolute;
}
.wrapper{
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

First of all, we will include all the required elements here like spans for timer and three buttons, This is what we have to add for our HTML file, also we have included our CSS and JS file here. Now let’s set this structure to the center, for that we have used text-align to center. And we have repositioned it with top and left property, Then we have used transform property to place all elements in the center of the screen. Of course, we have set our background with #222242 color.

How To Make a Stopwatch In Javascript

Styling and Animating The Timer

.wrapper p{
    position: relative;
    display: inline-block;
    color: #ffffff;
    z-index: 99;
    font-size: 48px;
    margin-bottom: 120px;
}
.wrapper p::before{
    content: '';
    position: absolute;
    width: 200px;
    height: 200px;
    background-color: #151538;
    z-index: -1;
    border-radius: 50%;
    left: -10%;
    top: -118%;
    animation-name: shine;
    animation-duration: 3s;
    animation-iteration-count: infinite;
}
@keyframes shine{
    0%,100%{
        box-shadow: 0px 0px 32px -12px rgba(246, 180, 0, .5);
    }
    50%{
        box-shadow: 0px 0px 32px 3px rgba(246, 180, 0, .5);
    }
}

Now lets style our timer, before that we have gave white color to timer, so it will visible. We have put the timer into the inline block, so the timer will be in a square block. Then we have provided z-index which is pretty huge number, but it is not necessary you can simply give any number more than 1.

After that, in before element we have gave some background color, and here we have z-index to -1, so this element would be behind of our timer and other elements. Now we have set border-radius to 50%, so the square block will get turn in to a circle. Lastly, we have added some animations to this block with 3 seconds duration for infinite times.

In animation part, we are just applying the box shadow with some colors.

How To Make a Stopwatch In Javascript

Styling Buttons

button{
    background-color: #222242;
    padding: 10px 38px;
    border: 1px solid #A9A9A9;
    border-radius: 28px;
    color: #fff;
    transition: all .2s ease;
    outline: 0;
}
button:not(:last-child){
    margin-right: 20px;
}
button:hover,
button:focus
{
    border-color: #F6B400;
    color: #F6B400;
    box-shadow: 0px 4px 27px -12px #F6B400;
}

Now for buttons, we have gave same basic styles, and we have also added margin from right except last button. Also, we have added focus and hover effects we with border, color and box-shadow colors.

How To Make a Stopwatch In Javascript

Adding Functionalities To Buttons

let seconds = 00;
let tens = 00;
let mins = 00;
let getSeconds = document.querySelector('.seconds');
let getTens = document.querySelector('.tens');
let getMins = document.querySelector('.mins');
let btnStart = document.querySelector('.btn-start');
let btnStop = document.querySelector('.btn-stop');
let btnReset = document.querySelector('.btn-reset');
let interval;

btnStart.addEventListener('click', () => {
    clearInterval(interval);
    set = setInterval(startTimer, 10);
})
btnStop.addEventListener('click', () => {
    clearInterval(set);
})
btnReset.addEventListener('click', () => {
    clearInterval(set);
    tens = '00';
    seconds = '00';
    mins = '00';
    getSeconds.innerHTML = seconds;
    getTens.innerHTML = tens;
    getMins.innerHTML = mins;
})

function startTimer(){
    tens++;
    if(tens <= 9){
        getTens.innerHTML = '0' + tens;
    }
    if(tens > 9){
        getTens.innerHTML = tens;
    }
    if(tens > 99){
        seconds++;
        getSeconds.innerHTML = '0' + seconds;
        tens = 0;
        getTens.innerHTML = '0' + 0;
    }
    if(seconds > 9){
        getSeconds.innerHTML = seconds;
    }
    if(seconds > 59){
        mins++;
        getMins.innerHTML = '0' + mins;
        seconds = 0;
        getSeconds.innerHTML = '0' + 0;
    }
    if(mins > 9){
        getSeconds.innerHTML = mins;
    }
}

In JavaScript part, we have got all necessary elements using querySelector. Now for start button, we have added an onClick event listener in which we are calling a function startTimer. In this function, we are checking conditions for tens if they are less than 9 then we will put the 0 before the tens, if it is greater than 9 then we don’t need 0 before it.

If tens greater than 99 then we need to increment a second and reset tens to 0, also we require 0 before the seconds if it is less than 9. After that, if seconds greater than 9 then we will put seconds as it is. And seconds passes the 59 then we will increment the minute. Again we will put the 0 if minute less than 9 otherwise we will remove it.

For stop button, we just need to stop the timer, we have used here clearInterval(set) to stop timer. And lastly, we need to reset timer, for that we have assigned 0 value to each of the value, and we have printed on screen using innerHTML.

How To Make a Stopwatch In Javascript

Full Source Code of How To Make a Stopwatch In JavaScript

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stopwatch</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <p>
                <span class="mins">00</span>:<span class="seconds">00</span>:<span class="tens">00</span>
            </p> <br>
            <button class="btn-start">Start</button>
            <button class="btn-stop">Stop</button>
            <button class="btn-reset">Reset</button>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

style.css

@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
*{
    margin: 0;
    padding: 0;
    font-family: 'Source Sans Pro', sans-serif;
}
.container{
    background-color: #222242;
    height: 100vh;
    width: 100%;
    text-align: center;
    position: absolute;
}
.wrapper{
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

.wrapper p{
    position: relative;
    display: inline-block;
    color: #ffffff;
    z-index: 1;
    font-size: 48px;
    margin-bottom: 120px;
}
.wrapper p::before{
    content: '';
    position: absolute;
    width: 200px;
    height: 200px;
    background-color: #151538;
    z-index: -1;
    border-radius: 50%;
	left: -10%;
    top: -118%;
    animation: shine 3s infinite;
}
@keyframes shine{
    0%,100%{
        box-shadow: 0px 0px 32px -12px rgba(246, 180, 0, .5);
    }
    50%{
        box-shadow: 0px 0px 32px 3px rgba(246, 180, 0, .5);
    }
}

button{
    background-color: #222242;
    padding: 10px 38px;
    border: 1px solid #A9A9A9;
    border-radius: 28px;
    color: #fff;
    transition: all .2s ease;
    outline: 0;
}
button:not(:last-child){
    margin-right: 20px;
}
button:hover,
button:focus
{
    border-color: #F6B400;
    color: #F6B400;
    box-shadow: 0px 4px 27px -12px #F6B400;
}

script.js

let seconds = 00;
let tens = 00;
let mins = 00;
let getSeconds = document.querySelector('.seconds');
let getTens = document.querySelector('.tens');
let getMins = document.querySelector('.mins');
let btnStart = document.querySelector('.btn-start');
let btnStop = document.querySelector('.btn-stop');
let btnReset = document.querySelector('.btn-reset');
let interval;

btnStart.addEventListener('click', () => {
    clearInterval(interval);
    set = setInterval(startTimer, 10);
})
btnStop.addEventListener('click', () => {
    clearInterval(set);
})
btnReset.addEventListener('click', () => {
    clearInterval(set);
    tens = '00';
    seconds = '00';
    mins = '00';
    getSeconds.innerHTML = seconds;
    getTens.innerHTML = tens;
    getMins.innerHTML = mins;
})

function startTimer(){
    tens++;
    if(tens <= 9){
        getTens.innerHTML = '0' + tens;
    }
    if(tens > 9){
        getTens.innerHTML = tens;
    }
    if(tens > 99){
        seconds++;
        getSeconds.innerHTML = '0' + seconds;
        tens = 0;
        getTens.innerHTML = '0' + 0;
    }
    if(seconds > 9){
        getSeconds.innerHTML = seconds;
    }
    if(seconds > 59){
        mins++;
        getMins.innerHTML = '0' + mins;
        seconds = 0;
        getSeconds.innerHTML = '0' + 0;
    }
    if(mins > 9){
        getSeconds.innerHTML = mins;
    }
}

Output

How To Make a Stopwatch In Javascript
View Demo
Download Code

You may also like:

  • How To Make Number Guessing Game JavaScript
  • Build a Simple Number Memorising Game In JavaScript
  • Build a Simple Digital Clock In JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Stopwatch, Stopwatch In JavaScript, Stopwatch In JavaScript 2022
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Animated Nike Shoes How to Make Animated Nike Shoes Landing Page in HTML
Next Article Coffee Cup Loading Animation In CSS How To Make Coffee Cup Loading Animation In CSS
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?