|
1 | 1 | /* globals quotes, pickFromArray */ |
2 | 2 |
|
3 | | -// Constants for configuration |
4 | | -// Sets the time interval for the auto-play feature in milliseconds. |
5 | | -const AUTO_PLAY_INTERVAL_MS = 5000; // 5 seconds for testing/demo |
| 3 | +// Use a constant for the auto-play time to make it easy to change later. |
| 4 | +const AUTO_PLAY_INTERVAL_MS = 5000; |
6 | 5 |
|
7 | | -/** |
8 | | - * Variables to track the auto-play interval state. |
9 | | - */ |
| 6 | +// Defines variables to track the state of the app. |
| 7 | +// Uses 'let' because the timer ID will change when we start/stop auto-play. |
10 | 8 | let autoPlayIntervalId = null; |
11 | 9 |
|
12 | | -// DOM Elements |
13 | | -const quoteElement = document.querySelector("#quote"); |
14 | | -const authorElement = document.querySelector("#author"); |
15 | | -const newQuoteButton = document.querySelector("#new-quote"); |
16 | | -const autoPlaySwitch = document.querySelector("#auto-play-switch"); |
17 | | -const autoPlayStatus = document.querySelector("#auto-play-status"); |
| 10 | +// DOM Elements (fetched once to keep the code efficient) |
| 11 | +const quoteElement = document.getElementById("quote"); |
| 12 | +const authorElement = document.getElementById("author"); |
| 13 | +const newQuoteButton = document.getElementById("new-quote"); |
| 14 | +const autoPlaySwitch = document.getElementById("auto-play-switch"); |
| 15 | +const autoPlayStatus = document.getElementById("auto-play-status"); |
18 | 16 |
|
19 | 17 | /** |
20 | | - * Updates the displayed quote and author on the screen. |
21 | | - * Uses the global pickFromArray function and quotes array. |
| 18 | + * Selects and displays a new random quote from the quote list. |
| 19 | + * This function changes the text on the screen. |
22 | 20 | */ |
23 | 21 | function displayNewQuote() { |
| 22 | + // Selects a random quote object from the global 'quotes' array. |
24 | 23 | const randomQuote = pickFromArray(quotes); |
| 24 | + |
| 25 | + // Updates the HTML elements with the new quote text and author name. |
25 | 26 | quoteElement.innerText = randomQuote.quote; |
26 | 27 | authorElement.innerText = randomQuote.author; |
27 | 28 | } |
28 | 29 |
|
29 | 30 | /** |
30 | | - * Starts the auto-play functionality. |
31 | | - * Sets an interval to update the quote and shows the status indicator. |
| 31 | + * Starts the auto-play feature. |
| 32 | + * This sets up a timer to automatically change the quote every few seconds. |
32 | 33 | */ |
33 | 34 | function startAutoPlay() { |
34 | | - // Clear any existing interval just in case |
| 35 | + // Clears any existing timer first to prevent multiple timers running at once. |
35 | 36 | if (autoPlayIntervalId) { |
36 | 37 | clearInterval(autoPlayIntervalId); |
37 | 38 | } |
38 | 39 |
|
39 | | - // Show status |
| 40 | + // Shows the "auto-play: ON" status text to inform the user it is active. |
40 | 41 | autoPlayStatus.classList.add("active"); |
41 | 42 |
|
42 | | - // Set interval |
| 43 | + // Sets a repeating timer that calls 'displayNewQuote' every 5000 milliseconds (5 seconds). |
43 | 44 | autoPlayIntervalId = setInterval(function () { |
44 | 45 | displayNewQuote(); |
45 | 46 | }, AUTO_PLAY_INTERVAL_MS); |
46 | 47 | } |
47 | 48 |
|
48 | 49 | /** |
49 | | - * Stops the auto-play functionality. |
50 | | - * Clears the interval and hides the status indicator. |
| 50 | + * Stops the auto-play feature. |
| 51 | + * This cancels the timer so the quotes stop changing automatically. |
51 | 52 | */ |
52 | 53 | function stopAutoPlay() { |
| 54 | + // Checks if a timer exists, and if so, stops it. |
53 | 55 | if (autoPlayIntervalId) { |
54 | 56 | clearInterval(autoPlayIntervalId); |
55 | 57 | autoPlayIntervalId = null; |
56 | 58 | } |
57 | 59 |
|
58 | | - // Hide status |
| 60 | + // Hides the "auto-play: ON" status text. |
59 | 61 | autoPlayStatus.classList.remove("active"); |
60 | 62 | } |
61 | 63 |
|
62 | 64 | /** |
63 | | - * Toggles auto-play based on the checkbox state. |
64 | | - * @param event - The change event from the checkbox. |
| 65 | + * Handles the change event when the user toggles the auto-play switch. |
| 66 | + * Decides whether to start or stop auto-play based on the switch position. |
| 67 | + * |
| 68 | + * @param {Event} event - The event object from the click. |
65 | 69 | */ |
66 | 70 | function handleAutoPlayToggle(event) { |
| 71 | + // Checks if the toggle switch is currently 'checked' (on). |
67 | 72 | if (event.target.checked) { |
68 | 73 | startAutoPlay(); |
69 | 74 | } else { |
70 | 75 | stopAutoPlay(); |
71 | 76 | } |
72 | 77 | } |
73 | 78 |
|
74 | | -// Event Listeners |
75 | | - |
76 | | -// Handle "New Quote" button click |
| 79 | +// Adds an event listener to the "New Quote" button. |
| 80 | +// When clicked, it shows a new quote and resets the auto-play timer if it's running. |
77 | 81 | newQuoteButton.addEventListener("click", function () { |
78 | 82 | displayNewQuote(); |
79 | 83 |
|
80 | | - // If auto-play is on, reset the timer so it doesn't change immediately after manual click |
| 84 | + // If auto-play is turned on, restarts the timer. |
| 85 | + // This prevents the quote from changing immediately after the user manually clicks. |
81 | 86 | if (autoPlaySwitch.checked) { |
82 | 87 | startAutoPlay(); |
83 | 88 | } |
84 | 89 | }); |
85 | 90 |
|
86 | | -// Handle Auto-play switch toggle |
| 91 | +// Adds an event listener to the auto-play toggle switch. |
| 92 | +// When changed, it triggers the handleAutoPlayToggle function. |
87 | 93 | autoPlaySwitch.addEventListener("change", handleAutoPlayToggle); |
88 | 94 |
|
89 | | -// Initial Load |
90 | | -// Display a random quote when the app starts |
| 95 | +// Displays a random quote immediately when the page loads so it isn't empty. |
91 | 96 | displayNewQuote(); |
0 commit comments