Stopwatch using HTML, CSS & JavaScript

Create StopWatch using HTML CSS and JavaScript

Create StopWatch using HTML CSS and JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. In this project, we will create a Digital Stopwatch which is used to record the time taken to perform an activity and exercise. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment. At last we will use JS (JavaScript) which will add a logic to make the project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Stopwatch

<html lang="en">
<head>
    <title>Stopwatch</title>
    <!--Google Font-->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600&family=Roboto+Mono:wght@700&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="timerDisplay">
            00 : 00 : 00 : 000
        </div>
        <div class="buttons">
            <button id="startTimer">Start</button>
            <button id="pauseTimer">Pause</button>
            <button id="resetTimer">Reset</button>
        </div>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the project for that as you can see the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

Portfolio Website using HTML and CSS (Source Code)

CSS Code for Stopwatch

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
.container{
    background-color: red;
    width: 40%;
    min-width: 500px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    padding: 60px 0;
    border-radius: 10px;
}
.timerDisplay{
    position: relative;
    width: 110%;
    background: linear-gradient(
        -135deg,
        #0048ce,
        #008bfd
    );
    left: -5%;
    padding: 40px 0;
    font-family: 'Roboto mono',monospace;
    color: #ffffff;
    font-size: 40px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    border-radius: 5px;
    box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
.buttons{
    width: 80%;
    margin: 60px auto 0 auto;
    display: flex;
    justify-content: space-around;
}
.buttons button{
    padding: 10px 20px;
    background-color: #ffffff;
    color: #141313;
    border: none;
    font-family: 'Poppins',sans-serif;
    font-size: 18px;
    border-radius: 5px;
    cursor: pointer;
    outline: none;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

Creating A Password Generator Using Javascript

JavaScript Code for Stopwatch

let [milliseconds,seconds,minutes,hours] = [0,0,0,0];
let timerRef = document.querySelector('.timerDisplay');
let int = null;

document.getElementById('startTimer').addEventListener('click', ()=>{
    if(int!==null){
        clearInterval(int);
    }
    int = setInterval(displayTimer,10);
});

document.getElementById('pauseTimer').addEventListener('click', ()=>{
    clearInterval(int);
});

document.getElementById('resetTimer').addEventListener('click', ()=>{
    clearInterval(int);
    [milliseconds,seconds,minutes,hours] = [0,0,0,0];
    timerRef.innerHTML = '00 : 00 : 00 : 000 ';
});

function displayTimer(){
    milliseconds+=10;
    if(milliseconds == 1000){
        milliseconds = 0;
        seconds++;
        if(seconds == 60){
            seconds = 0;
            minutes++;
            if(minutes == 60){
                minutes = 0;
                hours++;
            }
        }
    }
    let h = hours < 10 ? "0" + hours : hours;
    let m = minutes < 10 ? "0" + minutes : minutes;
    let s = seconds < 10 ? "0" + seconds : seconds;
    let ms = milliseconds < 10 ? "00" + milliseconds : milliseconds < 100 ? "0" + milliseconds : milliseconds;

    timerRef.innerHTML = ` ${h} : ${m} : ${s} : ${ms}`;
}

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. We have also defined function for each button and how the time will display. Let us see the Final Output of the project Digital Stopwatch using HTML, CSS & JavaScript (Source Code) | Stopwatch.

Weather App Using Html,Css And JavaScript 

Final Output Of StopWatch using HTML CSS and JavaScript

We have Successfully created our Stopwatch using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply