11/* globals quotes, pickFromArray */
22
3- // Defines the auto-play interval duration (60 seconds) as a constant for maintainability.
43const AUTO_PLAY_INTERVAL_MS = 60000 ;
5-
6- // Tracks the state of the auto-play timer.
7- // Initializes as null to indicate no active timer.
84let autoPlayIntervalId = null ;
95
10- // Caches DOM references to avoid repeated lookups during runtime.
116const quoteElement = document . getElementById ( 'quote' ) ;
127const authorElement = document . getElementById ( 'author' ) ;
138const newQuoteButton = document . getElementById ( 'new-quote' ) ;
149const autoPlaySwitch = document . getElementById ( 'auto-play-switch' ) ;
1510const autoPlayStatus = document . getElementById ( 'auto-play-status' ) ;
1611
17- // Retrieves a random quote from the data source and updates the DOM.
18- // Updates both the quote text and the author name.
1912function displayNewQuote ( ) {
20- // Selects a random quote object from the global 'quotes' array.
2113 const randomQuote = pickFromArray ( quotes ) ;
22-
23- // Updates the HTML elements with the new quote text and author name.
2414 quoteElement . innerText = randomQuote . quote ;
2515 authorElement . innerText = randomQuote . author ;
2616}
2717
28- // Initiates the auto-play feature.
29- // Establishes a recurring timer to refresh the displayed quote.
3018function startAutoPlay ( ) {
31- // Clears any existing timer to prevent overlapping intervals.
3219 if ( autoPlayIntervalId ) {
3320 clearInterval ( autoPlayIntervalId ) ;
3421 }
35-
36- // Activates the visual status indicator for auto-play.
3722 autoPlayStatus . classList . add ( 'active' ) ;
38-
39- // Schedules the 'displayNewQuote' function to execute every 60 seconds.
40- autoPlayIntervalId = setInterval ( function ( ) {
41- displayNewQuote ( ) ;
42- } , AUTO_PLAY_INTERVAL_MS ) ;
23+ autoPlayIntervalId = setInterval ( displayNewQuote , AUTO_PLAY_INTERVAL_MS ) ;
4324}
4425
45- // Terminates the auto-play feature.
46- // Clears the active timer and resets the state.
4726function stopAutoPlay ( ) {
48- // Verifies if a timer exists before attempting to clear it.
4927 if ( autoPlayIntervalId ) {
5028 clearInterval ( autoPlayIntervalId ) ;
5129 autoPlayIntervalId = null ;
5230 }
53-
54- // Deactivates the visual status indicator.
5531 autoPlayStatus . classList . remove ( 'active' ) ;
5632}
5733
58- // Manages the state change when the auto-play switch is toggled.
59- // Routes execution to start or stop auto-play based on the switch state.
6034function handleAutoPlayToggle ( event ) {
61- // Evaluates the checked state of the toggle switch.
6235 if ( event . target . checked ) {
6336 startAutoPlay ( ) ;
6437 } else {
6538 stopAutoPlay ( ) ;
6639 }
6740}
6841
69- // Attaches a click event listener to the "New quote" button.
70- // Triggers a manual update of the quote and resets the auto-play timer if active.
7142newQuoteButton . addEventListener ( 'click' , function ( ) {
7243 displayNewQuote ( ) ;
73-
74- // Restarts the auto-play timer if the feature is currently enabled.
75- // Ensures the full interval elapses before the next automatic update.
7644 if ( autoPlaySwitch . checked ) {
7745 startAutoPlay ( ) ;
7846 }
7947} ) ;
8048
81- // Attaches a change event listener to the auto-play toggle switch.
82- // Invokes the handler function whenever the user interacts with the switch.
8349autoPlaySwitch . addEventListener ( 'change' , handleAutoPlayToggle ) ;
8450
85- // Initializes the view by displaying a random quote upon page load.
86- displayNewQuote ( ) ;
51+ // Initial display on load.
52+ displayNewQuote ( ) ;
0 commit comments