@@ -519,11 +519,18 @@ const quotes = [
519519
520520// call pickFromArray with the quotes array to check you get a random quote
521521
522+ const autoPlayToggle = document . getElementById ( "auto-play-toggle" ) ;
523+ const autoPlayStatus = document . getElementById ( "auto-play-status" ) ;
522524
523525const quoteElement = document . getElementById ( "quote" ) ;
524526const authorElement = document . getElementById ( "author" ) ;
525527const newQuoteButton = document . getElementById ( "new-quote" ) ;
526528
529+ // Variable to store the timer interval ID
530+ let autoPlayInterval = null ;
531+
532+
533+ // 2. Define the function to get and display a new random quote
527534function renderQuote ( ) {
528535 // Use the provided pickFromArray helper function
529536 const randomQuote = pickFromArray ( quotes ) ;
@@ -533,6 +540,30 @@ function renderQuote() {
533540 authorElement . textContent = `- ${ randomQuote . author } ` ;
534541}
535542
543+ // 3. Functions to manage auto-play state
544+ function startAutoPlay ( ) {
545+ autoPlayStatus . textContent = "auto-play:ON" ;
546+
547+ // Set interval to change quote every 60 seconds (60000ms)
548+ autoPlayInterval = setInterval ( renderQuote , 5000 ) ;
549+ }
550+
551+ function stopAutoPlay ( ) {
552+ autoPlayStatus . textContent = "auto-play:OFF" ;
553+
554+ // Clear the active timer interval
555+ clearInterval ( autoPlayInterval ) ;
556+ autoPlayInterval = null ;
557+ }
558+
559+ autoPlayToggle . addEventListener ( "change" , ( event ) => {
560+ if ( event . target . checked ) {
561+ startAutoPlay ( ) ;
562+ } else {
563+ stopAutoPlay ( ) ;
564+ }
565+ } ) ;
566+
536567renderQuote ( ) ;
537568
538569newQuoteButton . addEventListener ( "click" , renderQuote ) ;
0 commit comments