Skip to content

Commit fb496a3

Browse files
authored
Implement auto-play functionality for quotes
Added auto-play feature for displaying random quotes every 5 seconds.
1 parent 5a28826 commit fb496a3

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Sprint-3/quote-generator/quotes.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

523525
const quoteElement = document.getElementById("quote");
524526
const authorElement = document.getElementById("author");
525527
const 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
527534
function 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+
536567
renderQuote();
537568

538569
newQuoteButton.addEventListener("click", renderQuote);

0 commit comments

Comments
 (0)