|
| 1 | +/* globals quotes, pickFromArray */ |
| 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 |
| 6 | + |
| 7 | +/** |
| 8 | + * Variables to track the auto-play interval state. |
| 9 | + */ |
| 10 | +let autoPlayIntervalId = null; |
| 11 | + |
| 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"); |
| 18 | + |
| 19 | +/** |
| 20 | + * Updates the displayed quote and author on the screen. |
| 21 | + * Uses the global pickFromArray function and quotes array. |
| 22 | + */ |
| 23 | +function displayNewQuote() { |
| 24 | + const randomQuote = pickFromArray(quotes); |
| 25 | + quoteElement.innerText = randomQuote.quote; |
| 26 | + authorElement.innerText = randomQuote.author; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Starts the auto-play functionality. |
| 31 | + * Sets an interval to update the quote and shows the status indicator. |
| 32 | + */ |
| 33 | +function startAutoPlay() { |
| 34 | + // Clear any existing interval just in case |
| 35 | + if (autoPlayIntervalId) { |
| 36 | + clearInterval(autoPlayIntervalId); |
| 37 | + } |
| 38 | + |
| 39 | + // Show status |
| 40 | + autoPlayStatus.classList.add("active"); |
| 41 | + |
| 42 | + // Set interval |
| 43 | + autoPlayIntervalId = setInterval(function () { |
| 44 | + displayNewQuote(); |
| 45 | + }, AUTO_PLAY_INTERVAL_MS); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Stops the auto-play functionality. |
| 50 | + * Clears the interval and hides the status indicator. |
| 51 | + */ |
| 52 | +function stopAutoPlay() { |
| 53 | + if (autoPlayIntervalId) { |
| 54 | + clearInterval(autoPlayIntervalId); |
| 55 | + autoPlayIntervalId = null; |
| 56 | + } |
| 57 | + |
| 58 | + // Hide status |
| 59 | + autoPlayStatus.classList.remove("active"); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Toggles auto-play based on the checkbox state. |
| 64 | + * @param event - The change event from the checkbox. |
| 65 | + */ |
| 66 | +function handleAutoPlayToggle(event) { |
| 67 | + if (event.target.checked) { |
| 68 | + startAutoPlay(); |
| 69 | + } else { |
| 70 | + stopAutoPlay(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Event Listeners |
| 75 | + |
| 76 | +// Handle "New Quote" button click |
| 77 | +newQuoteButton.addEventListener("click", function () { |
| 78 | + displayNewQuote(); |
| 79 | + |
| 80 | + // If auto-play is on, reset the timer so it doesn't change immediately after manual click |
| 81 | + if (autoPlaySwitch.checked) { |
| 82 | + startAutoPlay(); |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +// Handle Auto-play switch toggle |
| 87 | +autoPlaySwitch.addEventListener("change", handleAutoPlayToggle); |
| 88 | + |
| 89 | +// Initial Load |
| 90 | +// Display a random quote when the app starts |
| 91 | +displayNewQuote(); |
0 commit comments