@@ -499,25 +499,40 @@ const autoplayToggle = document.querySelector("#autoplay-toggle");
499499const autoplayStatus = document . querySelector ( "#autoplay-status" ) ;
500500
501501let autoplayId = null ;
502+ let currentQuote = null ;
502503
504+ // Picks and displays a random quote, avoiding the same quote twice in a row.
503505function showRandomQuote ( ) {
504- const randomQuote = pickFromArray ( quotes ) ;
506+ let randomQuote = pickFromArray ( quotes ) ;
507+
508+ if ( quotes . length > 1 ) {
509+ while ( randomQuote === currentQuote ) {
510+ randomQuote = pickFromArray ( quotes ) ;
511+ }
512+ }
513+
514+ currentQuote = randomQuote ;
505515 quoteP . innerText = randomQuote . quote ;
506516 authorP . innerText = randomQuote . author ;
507517}
508518
519+ // Updates the status text to reflect whether auto-play is on or off.
509520function setAutoplayStatus ( isEnabled ) {
510521 autoplayStatus . innerText = isEnabled ? "auto-play:ON" : "auto-play:OFF" ;
511522}
512523
524+ // Starts auto-play and immediately shows a new quote as user feedback.
513525function startAutoplay ( ) {
514526 if ( autoplayId !== null ) {
515527 clearInterval ( autoplayId ) ;
516528 }
529+
530+ showRandomQuote ( ) ;
517531 autoplayId = setInterval ( showRandomQuote , 60000 ) ;
518532 setAutoplayStatus ( true ) ;
519533}
520534
535+ // Stops auto-play if it is running and updates the status text.
521536function stopAutoplay ( ) {
522537 if ( autoplayId !== null ) {
523538 clearInterval ( autoplayId ) ;
@@ -526,6 +541,7 @@ function stopAutoplay() {
526541 setAutoplayStatus ( false ) ;
527542}
528543
544+ // Handles checkbox changes to start or stop auto-play.
529545function handleAutoplayToggle ( ) {
530546 if ( autoplayToggle . checked ) {
531547 startAutoplay ( ) ;
0 commit comments