JS and HTML - Quota Publishing

Asked

Viewed 41 times

1

I created a javascript code where shows me several Andom texts, but these only change with the refresh of the page.

And I wanted them to change every five seconds without doing refresh.

Javascript:

(function() {
  var quotes = [
    {
      text: " HEY ITS ME",
    },                    
    {
      text: " HEY YOU TO",
    }          

  ];
  var quote = quotes[Math.floor(Math.random() * quotes.length)];
  document.getElementById("quote").innerHTML =
    '<i class="fas fa-graduation-cap"></i> ' + quote.text + '';
})();
  • Why an array of objects and not an array of strings ?

2 answers

1

  • Give a more detailed description using the question example, or move to the comments

1


var div = document.getElementById("quote");
var quotes = [
  {
   text: " HEY ITS ME",
  },                    
  {
   text: " HEY YOU TO",
  }          
];

function random() {
   var quote = quotes[Math.floor(Math.random() * quotes.length)];
   div.innerHTML = '<i class="fas fa-graduation-cap"></i> ' + quote.text +'';
}

setInterval(random, 5000);
<div id="quote"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.