--------------
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Alarm Clock!</title>
</head>
<body>
<h1>Alarm Clock!</h1>
<form>
<!-- <div class="form-group">
<label for="exampleInputEmail1">Enter alram date and time</label>
<input type="text" class="form-control" id="alarm" aria-describedby="emailHelp" placeholder="Enter data and time in format YYYY-MM-DD-HH:MM:SS">
<small id="emailHelp" class="form-text text-muted">We'll never share your alarm time.</small>
</div> -->
<div class="form-group" id="name1">
<label for="exampleFormControlInput1">Enter alram date and time</label>
<input type="name" class="form-control" id="alarm" placeholder="Enter data and time in format YYYY-MM-DD-HH:MM:SS"/>
<div class="invalid-feedback">
Please provide a valid name
</div>
<div class="valid-feedback">
Valid name entered
</div>
</div>
<button type="submit" id="alarmSubmit" class="btn btn-primary">Set Alarm</button>
<!-- new one -->
</form>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="index.js"></script>
</body>
</html>
index.js:
------------
console.log("alarm clockkkk");
const alarmSubmit = document.getElementById("alarmSubmit");
let alarmText = document.getElementById("alarm");
alarmSubmit.addEventListener("click", setAlarm);
function setAlarm(e) {
e.preventDefault();
let regex = /^([0-9]){4}-([0-9]){2}-([0-9]){2} ([0-9]){2}:([0-9]){2}:([0-9]){2}/;
let str = alarmText.value;
if (regex.test(str)) {
console.log("regex matched");
alarmTime = new Date(alarmText.value);
console.log(`setting alarm at ${alarmTime}`);
currentTime = new Date();
let timeToAlarm = alarmTime - currentTime; //after this time ring the alarm
console.log(`alarm will ring at ${timeToAlarm} `);
setTimeout(() => {
console.log("ringing now");
ringBell();
}, timeToAlarm);
} else {
console.log("enter valid date format");
}
}
// function to play the alarm tone
function ringBell() {
var audio = new Audio(
"https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3"
);
audio.play();
}
0 Comments