JS and HTML - Publication of Quotas

1

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

And I wanted them to change every 5 seconds without 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 + '';
})();
    
asked by anonymous 21.05.2018 / 02:22

2 answers

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>
    
21.05.2018 / 02:34
1

What you need is interval. This tutorial will help you: link

It will look something like this:

setInterval(function(){
  // sua função
}, 3000); // a cada 3 segundos'
    
21.05.2018 / 02:27