<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jay Ferg</title>
    <description>The latest articles on DEV Community by Jay Ferg (@unrealdevz).</description>
    <link>https://dev.to/unrealdevz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2175729%2F9b833c0b-e2dd-4840-a7e0-9243c675effe.jpg</url>
      <title>DEV Community: Jay Ferg</title>
      <link>https://dev.to/unrealdevz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/unrealdevz"/>
    <language>en</language>
    <item>
      <title>How to create timer Counter with Html, Css, and Javascript.</title>
      <dc:creator>Jay Ferg</dc:creator>
      <pubDate>Thu, 10 Oct 2024 01:29:30 +0000</pubDate>
      <link>https://dev.to/unrealdevz/how-to-create-timer-counter-with-html-css-and-javascript-26b6</link>
      <guid>https://dev.to/unrealdevz/how-to-create-timer-counter-with-html-css-and-javascript-26b6</guid>
      <description>&lt;p&gt;Building a timer counter is a great way to understand the interaction between HTML, CSS, and JavaScript. This post will guide you through the process of creating a simple timer that starts counting when a user clicks a button. The timer will display the elapsed time in hours, minutes, and seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: HTML Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Start by creating the basic HTML structure. You need a div to display the timer and a button to start it. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Timer Counter&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="styles.css"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;h1&amp;gt;Timer Counter&amp;lt;/h1&amp;gt;
        &amp;lt;div id="timer"&amp;gt;00:00:00&amp;lt;/div&amp;gt;
        &amp;lt;button id="startButton"&amp;gt;Start Timer&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this HTML file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A div with id="timer" is used to display the timer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A button with id="start Button" is used to start the timer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: CSS Styling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, style the HTML elements to create a visually appealing interface. Feel free to style it to your liking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* styles.css */
body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.container {
    text-align: center;
}

#timer {
    font-size: 3rem;
    margin-bottom: 20px;
}

button {
    padding: 10px 20px;
    font-size: 1rem;
    cursor: pointer;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This CSS file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Centers the content both vertically and horizontally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Styles the timer display with a large font size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adds padding and styling to the button to make it more clickable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: JavaScript Logic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, add the JavaScript to handle the timer functionality. This includes starting the timer, updating it every second, and displaying the elapsed time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// script.js
document.addEventListener("DOMContentLoaded", () =&amp;gt; {
    const startButton = document.getElementById("startButton");
    const timerDisplay = document.getElementById("timer");
    let timerInterval;
    let startTime;

    function startTimer() {
        startTime = new Date();
        timerInterval = setInterval(updateTimer, 1000);
    }

    function updateTimer() {
        const currentTime = new Date();
        const elapsedTime = new Date(currentTime - startTime);

        const hours = String(elapsedTime.getUTCHours()).padStart(2, '0');
        const minutes = String(elapsedTime.getUTCMinutes()).padStart(2, '0');
        const seconds = String(elapsedTime.getUTCSeconds()).padStart(2, '0');

        timerDisplay.textContent = `${hours}:${minutes}:${seconds}`;
    }

    startButton.addEventListener("click", startTimer);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This JavaScript file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Waits for the DOM to load before executing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Defines the startTimer function to initialize the start time and set an&lt;br&gt;
interval to update the timer every second.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Defines the updateTimer function to calculate the elapsed time, format &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it, and update the timer display.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adds an event listener to the button to start the timer when clicked.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bonus: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hide the start button and display a stop button script&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// script.js
document.addEventListener("DOMContentLoaded", () =&amp;gt; {
    const startButton = document.getElementById("startButton");
    const stopButton = document.getElementById("stopButton");
    const timerDisplay = document.getElementById("timer");
    let timerInterval;
    let startTime;

    function startTimer() {
        startTime = new Date();
        timerInterval = setInterval(updateTimer, 1000);
        startButton.style.display = "none";
        stopButton.style.display = "inline-block";
    }

    function stopTimer() {
        clearInterval(timerInterval);
        startButton.style.display = "inline-block";
        stopButton.style.display = "none";
    }

    function updateTimer() {
        const currentTime = new Date();
        const elapsedTime = new Date(currentTime - startTime);

        const hours = String(elapsedTime.getUTCHours()).padStart(2, '0');
        const minutes = String(elapsedTime.getUTCMinutes()).padStart(2, '0');
        const seconds = String(elapsedTime.getUTCSeconds()).padStart(2, '0');

        timerDisplay.textContent = `${hours}:${minutes}:${seconds}`;
    }

    startButton.addEventListener("click", startTimer);
    stopButton.addEventListener("click", stopTimer);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
