Bar that changes from time to time

Asked

Viewed 56 times

0

Guys, I have a little bar on my website

<div style="padding: 30px;" class="bg-light-gray" id="barra- programas"></div>

I want to use JS to make her change text every 10 seconds, on

/sistemas/frases.php 

When accesses it comes with a different sentence, every 10 seconds I want js to take a sentence from there and put it in the bar, eliminating the previous message.

  • How will you find this text in PHP? Load the page already with the text/word array in a variable or use ajax?

  • I want a setTimeout every 10 seconds, every 10 seconds goes by, then it accesses a text from an Aray and puts the text there.

  • Will this php text be dynamic? will take from the database? if not why not put it straight into js

  • No, if you want it can be in an array in the same JS!

1 answer

2


You can do it like this:

var bar = ['texto 1', 'texto 2', 'texto 3'];
var start = 0;

function foo() {
  if (start == bar.length) {
    start = 0;
  }
  document.getElementById('qux').innerHTML = bar[start];
  start++;
}

setInterval(foo, 1000);
#qux {
  background: black;
  color: white;
  font-size: 40px;
  text-align: center;
}
<div id="qux"></div>

  • Thanks, it worked!!!!!

  • Try to take a look at logic to see if you understand, if you don’t understand any part ask me!

  • Buddy, there’s a fade to get past the text??

Browser other questions tagged

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