Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

ProgrammingJQuery

Design a Digital Clock with Alarm Using jQuery

By Admin
December 4, 2024 3 Min Read
0

In this tutorial, we’ll learn how to create a digital clock with an alarm feature using HTML, CSS, and jQuery. This is an interactive project where you can set multiple alarms, remove them, and stop the alarm sound when it rings.

Features of the Digital Clock with Alarm

  • Real-Time Clock: Displays the current time in a clean and stylish format.
  • Set Alarms: Users can set alarms using an intuitive interface.
  • Alarm Notifications: A sound plays when the current time matches any set alarm.
  • Manage Alarms: Options to stop the alarm and remove alarms from the list dynamically.

Follow this video for complete guidance:

https://www.youtube.com/watch?v=uZDr7caLnGo

Complete Source Code

Here’s the complete code. You can copy and paste it into your project to see it in action.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Digital Clock & Alarm</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      background-color: #282c34;
      color: #61dafb;
      margin: 0;
      padding: 0;
    }

    .container {
      margin-top: 50px;
      padding: 20px;
    }

    #clock {
      font-size: 48px;
      margin-bottom: 20px;
    }

    input, button {
      margin: 10px 5px;
      padding: 10px;
      border: none;
      border-radius: 5px;
      font-size: 16px;
    }

    input {
      width: 150px;
    }

    button {
      background-color: #61dafb;
      color: #282c34;
      cursor: pointer;
    }

    button:hover {
      background-color: #21a1f1;
    }

    .alarm-list {
      width:50%;
      margin:0 auto;
    }

    .alarm-item {
      margin: 15px 0;
      padding: 5px;
      background: #444;
      border-radius: 5px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      color: white;
    }

    .stop-btn {
      display: none;
      margin-top: 20px;
      padding: 10px;
      background: red;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }

    .stop-btn:hover {
      background: darkred;
    }
  </style>
</head>
<body>
  <div class="container">
    <div id="clock">00:00:00</div>
    <input type="time" id="alarmInput" />
    <button id="setAlarmBtn">Set Alarm</button>
    <div class="alarm-list" id="alarmList"></div>
    <button class="stop-btn" id="stopAlarmBtn">Stop Alarm</button>
  </div>

  <audio id="alarmSound" src="alarm.mp3" preload="auto"></audio>
<script>
  $(function () {
    const alarms = [];
    const $clock = $('#clock');
    const $alarmInput = $('#alarmInput');
    const $alarmList = $('#alarmList');
    const $stopAlarmBtn = $('#stopAlarmBtn');
    const alarmSound = $('#alarmSound')[0];

    // Update clock
    setInterval(() => {
      const now = new Date();
      $clock.text(now.toLocaleTimeString('en-GB'));
      const currentTime = now.toTimeString().slice(0, 5);
      if (alarms.includes(currentTime)) {
        alarmSound.play();
        $stopAlarmBtn.show();
        alarms.splice(alarms.indexOf(currentTime), 1);
        $alarmList.find(`[data-time="${currentTime}"]`).remove();
      }
    }, 1000);

    // Add alarm
    $('#setAlarmBtn').on('click', () => {
      const time = $alarmInput.val();
      if (time && !alarms.includes(time)) {
        alarms.push(time);
        $alarmList.append(`<div data-time="${time}" class="alarm-item">${time} <button class="remove-btn">Remove</button></div>`);
        $alarmInput.val('');
      } else {
        alert('Enter a valid time or avoid duplicates.');
      }
    });

    // Remove alarm
    $alarmList.on('click', '.remove-btn', function () {
      const time = $(this).parent().data('time');
      alarms.splice(alarms.indexOf(time), 1);
      $(this).parent().remove();
    });

    // Stop alarm
    $stopAlarmBtn.on('click', () => {
      alarmSound.pause();
      alarmSound.currentTime = 0;
      $stopAlarmBtn.hide();
    });
  });
</script>

</body>
</html>

Key Highlights of the Implementation

Interactive Real-Time Clock

The core feature of the project is the clock that updates every second to display the current time accurately. Using jQuery, the time is formatted and dynamically displayed on the page.

Alarm Functionality

Users can easily set alarms by selecting a specific time from a time input field. When the selected time matches the current time, an audio notification is triggered.

Dynamic Alarm List

The alarms are displayed as a list below the input field, allowing users to track the alarms they have set. Each alarm has a “Remove” button for easy management, ensuring the interface stays clean and user-friendly.

Visual Design and Responsiveness

The interface is designed with a modern, minimalistic style using CSS. Elements like buttons and the alarm list are styled with gradients and animations to enhance the user experience. The responsive design ensures compatibility across devices.

Stop Alarm Button

When the alarm is triggered, users can stop it by clicking the “Stop Alarm” button. This immediately halts the sound and hides the button until the next alarm is triggered.

Why Use jQuery for This Project?

jQuery simplifies DOM manipulation and event handling, making it a perfect choice for building interactive features like updating the clock in real-time and dynamically managing alarms.

This project is a fantastic way to practice combining HTML, CSS, and jQuery to create something both fun and functional. It’s also a great starting point for exploring advanced features, such as recurring alarms or snooze functionality.

By completing this project, you’ll not only enhance your understanding of jQuery but also learn how to design and implement real-world web applications. Happy coding!

Tags:

clockCSSdigital clockHTML
Author

Admin

Follow Me
Other Articles
Previous

Building a Custom URL Shortener Using PHP and MySQL

Next

Building an Online Voting System using PHP and MySQL

No Comment! Be the first one.

Leave a Reply

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

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme