Skip to main content

Building a Simple Stopwatch with HTML and JavaScript

 

In the fast-paced world of web development, it's crucial to have tools that help us manage time effectively. One such tool is a stopwatch, and in this tutorial, we'll walk through the process of building a simple stopwatch using HTML and JavaScript.

How We can create A simple stopwatch with use of HTML CSS and JavaScript.

Output:

stopwatch png
Output of project : Stopwatch


HTML Structure

To begin, let's take a look at the HTML structure that forms the foundation of our stopwatch:

html
<div class="stopWatch"> <div id="timer">00:00:00</div> <div id="buttons"> <button id="start">Start</button> <button id="stop">Stop</button><button id="reset">Reset</button> </div> </div>

Here, we have a container div with the class stopWatch that houses the display area (timer) and control buttons (start, stop, and reset

We recently create a structure of Our Projects Now Its Time to Design it With the help of CSS Okay Kindly Select the Whole Code and Paste it Your Styles.css File and Save it Successfully.

body {
  background-color: #f0f0f0;
  font-family: "Poppins", sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100vh;
  overflow: hidden;
  align-items: center;
}
.stopWatch{
  height: 300px;
  width: 70%;
  background-color: #223;
  border-radius: 10px;
  box-shadow: rgb(38, 57, 77) 0px 20px 30px -10px;
}
#timer {
  font-size: 7rem;
  font-weight: 700;
  text-shadow: 2px 2px #223;
  color: #f0f0f0;
  width: 600px;
  text-align: center;
  margin: 40px auto;
}

#buttons {
  display: flex;
  justify-content: center;
}

button {
  background-color: #fff;
  color: black;
  border: none;
  font-size: 20px;
  font-weight: 600;
  padding: 1.1rem 3rem;
  margin: 1rem;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: rgba(60, 64, 67, 0.3) 0px 1px 2px 0px, rgba(60, 64, 67, 0.15) 0px 2px 6px 2px;
  transition: all 0.2s;
  transition: 0.5s ease-in-out;
}

button:hover {
  background-color: rgb(44, 44, 66);
  color: #f0f0f0;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.5);
}



@media (max-width: 800px) {
  #timer {
    font-size: 4rem;
    width: 350px;
  }

  button {
    font-size: 1.1rem;
    padding: 1rem 1.5rem;
  }
}

JavaScript Implementation

Let's break down the JavaScript code that powers our enhanced stopwatch:

javascript
const timerEl = document.getElementById("timer"); const startButtonEl = document.getElementById("start"); const stopButtonEl = document.getElementById("stop"); const resetButtonEl = document.getElementById("reset"); let startTime = 0; let elapsedTime = 0; let timerInterval;

In this section, we set up variables to capture elements from the HTML and define variables to manage the stopwatch's state.

Start Timer Function

javascript
function startTimer() { startTime = Date.now() - elapsedTime; timerInterval = setInterval(() => { elapsedTime = Date.now() - startTime; timerEl.textContent = formatTime(elapsedTime); }, 10); startButtonEl.disabled = true; stopButtonEl.disabled = false; }

The startTimer function initializes the timer, capturing the start time and updating the elapsed time at regular intervals. It also disables the start button while enabling the stop button.

Format Time Function

javascript
function formatTime(elapsedTime) { // ... precise time formatting logic }

The formatTime function takes the elapsed time and formats it into hours, minutes, seconds, and milliseconds for a user-friendly display.

Stop and Reset Functions

javascript
function stopTimer() { clearInterval(timerInterval); startButtonEl.disabled = false; stopButtonEl.disabled = true; } function resetTimer() { clearInterval(timerInterval); elapsedTime = 0; timerEl.textContent = "00:00:00.00"; startButtonEl.disabled = false; stopButtonEl.disabled = true; }

The stopTimer function stops the timer and toggles button states, while the resetTimer function resets the timer to zero and updates the display accordingly.

Event Listeners

javascript
startButtonEl.addEventListener("click", startTimer); stopButtonEl.addEventListener("click", stopTimer); resetButtonEl.addEventListener("click", resetTimer);

Lastly, we set up event listeners for the start, stop, and reset buttons to trigger their respective functions.

Conclusion

Congratulations! You've successfully built an enhanced stopwatch with JavaScript, featuring precise time formatting and intuitive controls. Feel free to customize and integrate this stopwatch into your projects. Time is on your side!


Comments

Popular posts from this blog

10 + Mind-Blowing CSS Animation Examples (Free Code + Demos)

                                   10 + CSS Animations With Source Code  Welcome to our collection of CSS animations! In this curated collection, we have gathered a variety of free HTML and CSS animation code examples from reputable sources such as Code Pen, GitHub, and other valuable resources. With our November 2022 update, we are excited to present 18 new items that showcase the latest trends and techniques in web design. Whether you are a web developer seeking inspiration or a designer looking for ready-to-use animation snippets, this collection has something for everyone. Explore the power of CSS animations and enhance your website with captivating visual effects. 1. Snow Globe The coziest example we could find, this snow globe animation, adds an ambiance to your page that you won’t get with a still image. Notice how the snow animates only within the confines of the “glass...

Creating a Stunning 3D Carousel with HTML, CSS, and JavaScript

Creating a Stunning 3D Carousel with HTML, CSS,  and JavaScript In this blog post, we'll walk you through the process of building a mesmerizing 3D carousel using a combination of HTML, CSS, and JavaScript. Carousels are a popular element on websites, often used to showcase featured content or images in an engaging way. This particular carousel goes a step further, incorporating a 3D effect that adds depth and a sense of interactivity to your web page. Our tutorial will guide you through the step-by-step creation of this 3D carousel. We'll explain how to set up the HTML structure, apply the necessary CSS styles to achieve the 3D effect, and even provide a JavaScript template for handling user interactions (though this example keeps it simple). Whether you're a web development novice or an experienced coder looking for a creative project, you'll find this tutorial accessible and engaging. By the end of the blog post, you'll have a stunning 3D carousel that you can cus...

Responsive CSS Cards -Free Code + Demo

                   Responsive CSS Cards -Free Code + Demo                             All code 100% Free code and open source             How to create Responsive Cards Using HTML and CSS Here is the Example With Live Demo + Source Code            Output Sample Image                    CSS Card with Hover Effects Code Pen See the Pen Responsive Card With Hover Effects by Amol Kamble ( @amkamble ) on CodePen . Live / Source-Code <! DOCTYPE html > < html lang = "en" >   < head >     < meta charset = "UTF-8" />     < meta name = "viewport" content = "width=device-width, initial-scale=1.0" />     < title >Responsive Card Design HTML and C...