Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alarm clock in JavaScript #392

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions JavaScript/Alarm_Clock/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Alarm Clock

A website for alarm clock.

## How to use the script ?

Open index.html in any browser.

## Screenshot
![demo](https://user-images.githubusercontent.com/56690856/99620214-18af7d80-2a4b-11eb-8f07-dfbf52b9df48.png)
Binary file added JavaScript/Alarm_Clock/audio.mp3
Binary file not shown.
24 changes: 24 additions & 0 deletions JavaScript/Alarm_Clock/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alarm Clock</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<form>
<label for="alarm"><h1>Set Alarm</h1></label>
<h2>Enter the time in yyyy-mm-dd hh:mm:ss</h2>
<input type="text" id="alarm" name="alarm" placeholder="ENTER"></br>
<button id="alarmSubmit" type="submit" class="btn">Set Alarm</button>
<button id="alarmStop" type="submit" class="btn">Stop Alarm</button>
</form>
</div>
<audio id="alarmAudio">
<source src="audio.mp3" type="audio/mpeg">
</audio>
<script src="index.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions JavaScript/Alarm_Clock/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const alarmSubmit = document.getElementById("alarmSubmit");
const alarmStop = document.getElementById("alarmStop");
const audio = document.getElementById("alarmAudio");

// Add an event listener to the submit button
alarmSubmit.addEventListener("click", setAlarm);

// Add an event listener to the stop button
alarmStop.addEventListener("click", stopAlarm);

// function to play the alarm ring tone
function ringBell() {
audio.play();
}

// function to set alarm
function setAlarm(e) {
e.preventDefault();
const alarm = document.getElementById("alarm");
alarmDate = new Date(alarm.value);
now = new Date();
console.log(now);
let timeToAlarm = alarmDate - now;
console.log(timeToAlarm);
if (timeToAlarm >= 0) {
setTimeout(() => {
ringBell();
}, timeToAlarm);
}
}

// function to stop alarm
function stopAlarm(e) {
audio.pause();
}
38 changes: 38 additions & 0 deletions JavaScript/Alarm_Clock/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
body {
background-color: #fecd1a;
}

.container {
background-color: black;
color: white;
padding: 30px;
text-align: center;
margin: auto;
width: 50%;
margin-top: 10%;
border-radius: 30px;
}

h1{
font-size:60px
}

h2 {
font-size: 40px;
}

.btn{
margin: 25px;
font-size: 25px;
border-color: chartreuse;
border-width: 5px;
}

#alarm{
font-size: 30px;
padding: 10px;
}

::placeholder{
color: black;
}