Skip to content

Commit 77f235b

Browse files
authored
Merge pull request #392 from Sapna2001/alarm_clock
Alarm clock in JavaScript
2 parents ad7ac6d + f7a8ca9 commit 77f235b

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

JavaScript/Alarm_Clock/Readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Alarm Clock
2+
3+
A website for alarm clock.
4+
5+
## How to use the script ?
6+
7+
Open index.html in any browser.
8+
9+
## Screenshot
10+
![demo](https://user-images.githubusercontent.com/56690856/99620214-18af7d80-2a4b-11eb-8f07-dfbf52b9df48.png)

JavaScript/Alarm_Clock/audio.mp3

492 KB
Binary file not shown.

JavaScript/Alarm_Clock/index.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Alarm Clock</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<div class="container">
11+
<form>
12+
<label for="alarm"><h1>Set Alarm</h1></label>
13+
<h2>Enter the time in yyyy-mm-dd hh:mm:ss</h2>
14+
<input type="text" id="alarm" name="alarm" placeholder="ENTER"></br>
15+
<button id="alarmSubmit" type="submit" class="btn">Set Alarm</button>
16+
<button id="alarmStop" type="submit" class="btn">Stop Alarm</button>
17+
</form>
18+
</div>
19+
<audio id="alarmAudio">
20+
<source src="audio.mp3" type="audio/mpeg">
21+
</audio>
22+
<script src="index.js"></script>
23+
</body>
24+
</html>

JavaScript/Alarm_Clock/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const alarmSubmit = document.getElementById("alarmSubmit");
2+
const alarmStop = document.getElementById("alarmStop");
3+
const audio = document.getElementById("alarmAudio");
4+
5+
// Add an event listener to the submit button
6+
alarmSubmit.addEventListener("click", setAlarm);
7+
8+
// Add an event listener to the stop button
9+
alarmStop.addEventListener("click", stopAlarm);
10+
11+
// function to play the alarm ring tone
12+
function ringBell() {
13+
audio.play();
14+
}
15+
16+
// function to set alarm
17+
function setAlarm(e) {
18+
e.preventDefault();
19+
const alarm = document.getElementById("alarm");
20+
alarmDate = new Date(alarm.value);
21+
now = new Date();
22+
console.log(now);
23+
let timeToAlarm = alarmDate - now;
24+
console.log(timeToAlarm);
25+
if (timeToAlarm >= 0) {
26+
setTimeout(() => {
27+
ringBell();
28+
}, timeToAlarm);
29+
}
30+
}
31+
32+
// function to stop alarm
33+
function stopAlarm(e) {
34+
audio.pause();
35+
}

JavaScript/Alarm_Clock/style.css

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
body {
2+
background-color: #fecd1a;
3+
}
4+
5+
.container {
6+
background-color: black;
7+
color: white;
8+
padding: 30px;
9+
text-align: center;
10+
margin: auto;
11+
width: 50%;
12+
margin-top: 10%;
13+
border-radius: 30px;
14+
}
15+
16+
h1{
17+
font-size:60px
18+
}
19+
20+
h2 {
21+
font-size: 40px;
22+
}
23+
24+
.btn{
25+
margin: 25px;
26+
font-size: 25px;
27+
border-color: chartreuse;
28+
border-width: 5px;
29+
}
30+
31+
#alarm{
32+
font-size: 30px;
33+
padding: 10px;
34+
}
35+
36+
::placeholder{
37+
color: black;
38+
}

0 commit comments

Comments
 (0)