Open In App

Create a Stopwatch using Bootstrap & JavaScript

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A stopwatch tracks elapsed time, typically in hours, minutes, seconds, and fractions of a second. It can be started, stopped, and reset, providing precise timing for various activities, such as sports, experiments, or productivity tracking.

Screenshot-2024-02-25-085509

Prerequisites

Approach

  • HTML, Bootstrap, and JavaScript were used to create the stopwatch.
  • Bootstrap is used for layout, including card structure. JavaScript manages stopwatch functionality.
  • Time values are updated every 10 milliseconds.
  • Event listeners trigger actions for start, stop, and reset buttons.
  • The stopwatch accurately increments seconds, minutes, and hours.
  • Single-digit values padded with zeros for consistency. Provides a visually appealing interface.
  • Enables tracking of elapsed time in various contexts.
  • Combines styling and functionality for effective stopwatch implementation.

Example: This example shows the implementation of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Bootstrap Stopwatch</title>
    <link href=
</head>
 
<body>
    <div class="container mt-5">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-12 text-center">
                        <h1>Bootstrap Stopwatch</h1>
                    </div>
                </div>
                <div class="row mt-5">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="card text-white bg-primary">
                                    <div class="card-body text-center" id="time">
                                        <span class="digit" id="hr">00</span>
                                        <span class="txt">Hr</span>
                                        <span class="digit" id="min">00</span>
                                        <span class="txt">Min</span>
                                        <span class="digit" id="sec">00</span>
                                        <span class="txt">Sec</span>
                                        <span class="digit" id="count">00</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row mt-3 justify-content-center">
                    <button class="btn btn-success mx-2"
                            id="start">Start</button>
                    <button class="btn btn-primary mx-2"
                            id="stop">Stop</button>
                    <button class="btn btn-danger mx-2"
                            id="reset">Reset</button>
                </div>
            </div>
        </div>
    </div>
 
    <script>
        let startBtn = document.getElementById('start');
        let stopBtn = document.getElementById('stop');
        let resetBtn = document.getElementById('reset');
 
        let hour = 0;
        let minute = 0;
        let second = 0;
        let count = 0;
        let timer;
 
        startBtn.addEventListener('click', function () {
            // Check if timer is not already running
            if (!timer) {
                timer = setInterval(stopWatch, 10);
            }
        });
 
        stopBtn.addEventListener('click', function () {
            clearInterval(timer);
            timer = null; // Reset timer variable
        });
 
        resetBtn.addEventListener('click', function () {
            clearInterval(timer);
            timer = null; // Reset timer variable
            hour = 0;
            minute = 0;
            second = 0;
            count = 0;
            updateDisplay();
        });
 
        function stopWatch() {
            count++;
 
            if (count == 100) {
                second++;
                count = 0;
            }
 
            if (second == 60) {
                minute++;
                second = 0;
            }
 
            if (minute == 60) {
                hour++;
                minute = 0;
                second = 0;
            }
 
            updateDisplay();
        }
 
        function updateDisplay() {
            document.getElementById('hr').textContent =
                hour.toString().padStart(2, '0');
            document.getElementById('min').textContent =
                minute.toString().padStart(2, '0');
            document.getElementById('sec').textContent =
                second.toString().padStart(2, '0');
            document.getElementById('count').textContent =
                count.toString().padStart(2, '0');
        }
    </script>
 
</body>
 
</html>


Output:

darkMode



Previous Article
Next Article

Similar Reads

How to Create Stopwatch using HTML CSS and JavaScript ?
In this article, we will learn How to create a Stopwatch using HTML CSS, and JavaScript. The StopWatch will have the Start, Stop, and Reset functionality. Prerequisites:HTML CSS JavaScriptApproach:Create one container in which all the elements are present.Inside this container add 2 divs that contain all time elements like an hour, minutes, seconds
3 min read
Create a Stopwatch using Tailwind CSS & jQuery
A stopwatch tracks elapsed time with precision. It starts counting when activated, halts when stopped, and resets to zero upon reset. It measures time in hours, minutes, seconds, and fractions. Stopwatch functionality aids in various activities such as sports timing, scientific experiments, and productivity monitoring. PrerequisitesHTMLTailwind CSS
2 min read
Lap Memory Stopwatch using React
Stopwatch is an application which helps to track time in hours, minutes, seconds, and milliseconds. This application implements all the basic operations of a stopwatch such as start, pause and reset button. It has an additional feature using which we can keep a record of laps which is useful when we have to note time for certain checkpoints. Previe
5 min read
Create a Password Validator using Bootstrap & JavaScript
A password validator assesses password strength by verifying if it meets predetermined criteria like length, inclusion of uppercase and lowercase letters, digits, and symbols. It ensures robust security measures for user authentication and data protection. PrerequisitesHTMLBootstrapJavaScriptApproachThe provided code is a password strength checker
3 min read
Include Bootstrap in AngularJS using ng-bootstrap
AngularJS can be integrated with Bootstrap CSS and Javascript and can be used to create creative forms, tables, navigation bars, etc. Steps: Make sure the Angular CLI present in your system if not then run the command in the terminal to do so. npm install -g @angular/cli Create a new project on visual studio code by running the code ng new project-
2 min read
Create a Weather App in HTML Bootstrap & JavaScript
A weather app contains a user input field for the user, which takes the input of the city name. Once the user enters the city name and clicks on the button, then the API Request is been sent to the OpenWeatherMap and the response is been retrieved in the application which consists of weather, wind speed, description, humidity, pressure etc. Preview
3 min read
Create a Multi Step Form in Bootstrap 5 & JavaScript
Forms are the gateway for interaction between users and web services. As the complexity of data collection increases, so does the need for more user-friendly form interfaces. A multi-step form, by breaking down the data collection process into manageable chunks, enhances user experience and improves form completion rates. We will going to create a
4 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Difference Between Bootstrap 3 and Bootstrap 4
Bootstrap: Bootstrap was developed by Mark Otto and Jacob Thorton at Twitter in August 2011. It is an open-source framework that is used to design responsive websites faster and easier. It is the most popular open-source framework that includes HTML, CSS, and JavaScript. It can create web applications with any server-side Technology like Java, PHP,
2 min read
Difference between Bootstrap 4 and Bootstrap 5
What is Bootstrap? It is an open-source framework from late 2011 that is used for designing responsive websites with a mobile-first approach faster and easier. Bootstrap is available for HTML, CSS, and JS. According to server-side languages like PHP, Node, etc. bootstrap helps to design the frontend. Bootstrap has made the work for developers easie
3 min read