-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
90 lines (76 loc) · 3.3 KB
/
timer.js
File metadata and controls
90 lines (76 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Timer Application Script
*
* This script controls the countdown timer functionality, including start, stop, and restart actions. It also handles audio playback when the timer reaches zero.
*
* @file timer.js
* @author Nicky Moreno Gonzalez
* @description Implementation of a simple countdown timer with audio notification and alert.
*/
let timer; // Stores the interval ID for the countdown timer
let timeRemaining; // Stores the remaining time in seconds
let isRunning = false; // Tracks whether the timer is currently running
// Update the countdown display
function updateDisplay() {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
document.getElementById("countdown").textContent =
`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// Format as "MM:SS" with leading zeros
}
// Wait for the HTML document to fully load before executing this function
document.addEventListener("DOMContentLoaded", function () {
// Get the query parameters from the URL
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get("name"); // Extract the timer name
const duration = parseInt(urlParams.get("duration"), 10); // Convert duration to an integer
const description = urlParams.get("description"); // Extract the timer description
// Set the extracted values in the HTML page
document.getElementById("timer-name").textContent = name;
document.getElementById("timer-description").textContent = description;
// Store the extracted duration as the initial countdown value
timeRemaining = duration;
updateDisplay(); // Update the countdown display with the initial value
// Preload sound to ensure it can play when needed
document.getElementById("alarm-sound").play().then(() => {
document.getElementById("alarm-sound").pause(); // Immediately pause after playing
document.getElementById("alarm-sound").currentTime = 0; // Reset audio position
}).catch(error => {
console.log("Audio play may be blocked until user interaction:", error);
});
});
// Function to start the countdown timer
function startTimer() {
if (!isRunning) { // Only start if the timer is not already running
isRunning = true;
timer = setInterval(() => {
if (timeRemaining > 0) {
timeRemaining--;
updateDisplay();
} else {
clearInterval(timer); // Stop the countdown
isRunning = false;
playSound(); // Play the alarm sound
alert("Time's up!"); // Show an alert to the user
}
}, 1000); // Runs every second (1000ms)
}
}
// Stop the countdown timer
function stopTimer() {
clearInterval(timer);
isRunning = false;
}
// Restart the timer with the original duration
function restartTimer() {
stopTimer();
timeRemaining = parseInt(new URLSearchParams(window.location.search).get("duration"), 10); // Reset time
updateDisplay(); // Refresh the countdown display
}
// Function to play an alarm sound when the timer reaches zero
function playSound() {
const audio = document.getElementById("alarm-sound");
if (audio) {
audio.play(); // Attempt to play the sound
}
}