javascript exercises
javascript exercises and solutions
javascript exercises for beginners
javascript function exercises
javascript practice exercises with solutions
javascript tasks for practice
javascript variables exercises
js exercises
The setTimeout() function in JavaScript can be used to create a straightforward alarm clock that plays a sound or displays a message at a particular time. Here’s an illustration:
function setAlarm(alarmTime) {
var currentTime = new Date().toLocaleTimeString();
console.log("Current time: " + currentTime);
console.log("Alarm time: " + alarmTime);
var timeDiff = (new Date(alarmTime) - new Date()) / 1000; // Calculate time difference in seconds
if (timeDiff > 0) {
setTimeout(function() {
playSound(); // Play a sound
displayMessage("Wake up!"); // Display a message
}, timeDiff * 1000); // Convert time difference to milliseconds
}
}
function playSound() {
// Replace 'sound.mp3' with the path to your sound file
var audio = new Audio('sound.mp3');
audio.play();
}
function displayMessage(message) {
console.log(message);
}
// Set the alarm time (24-hour format, e.g., "08:00:00")
var alarmTime = "08:00:00";
// Start the alarm
setAlarm(alarmTime);
The desired alarm time is an argument to the setAlarm() function in this JavaScript example. It uses new Date() to determine the time difference between the current time and the alarm time, and setTimeout() to set the alarm to go off at the designated time. The playSound() function is used to play a sound once the alarm time has been reached, and the displayMessage() function is used to show a message.
You must substitute the path to your own sound file in MP3 format for “sound.mp3”. The message that appears when the alarm sounds can also be changed.
HTML code with JS
If you want to view the project in the browser you can use the HTML code
<!DOCTYPE html>
<html>
<head>
<title>Alarm Clock</title>
<script src="alarm_clock.js"></script>
</head>
<body>
<h1>Alarm Clock</h1>
<p>Set the alarm time:</p>
<input type="time" id="alarmTimeInput">
<button onclick="setAlarm()">Set Alarm</button>
</body>
</html>
Learn more
JavaScript ile neler yapılabilir? JavaScript öğrenmek ne kadar sürer? JavaScript hangi program? JavaScript neden popüler?¿Qué proyectos se pueden hacer con JavaScript? ¿Qué es un proyecto en JavaScript? ¿Cómo empezar un proyecto de JavaScript? ¿Qué programas se han hecho con JavaScript? Wird JavaScript noch benötigt? Was kann man alles mit JavaScript machen? Ist JavaScript für Anfänger? Wie schwierig ist JavaScript? مشاريع جافا سكريبت للمبتدئين مشاريع جافا سكريبت جاهزة pdf مشروع جافا سكريبت javascript مشروع جافا سكريبت github تفعيل جافا سكريبت على الهاتف مشاريع جافا للمبتدئين جافا سكريبت تحميل تحميل جافا سكريبت للاندرويد
0 Comments