Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript / jQuery or something to change text every some seconds

I need JavaScript or jQuery something to change text every few seconds... without the user doing anything.

Example:

"Welcome" changes to "Salmat datang" changes to "Namaste", etc. after 3 seconds and loops back.

like image 614
Neil Avatar asked Jun 18 '11 19:06

Neil


2 Answers

As others have said, setInterval is your friend:

var text = ["Welcome", "Hi", "Sup dude"];
var counter = 0;
var elem = document.getElementById("changeText");
var inst = setInterval(change, 1000);

function change() {
  elem.innerHTML = text[counter];
  counter++;
  if (counter >= text.length) {
    counter = 0;
    // clearInterval(inst); // uncomment this if you want to stop refreshing after one cycle
  }
}
<div id="changeText"></div>
like image 120
Thomas Shields Avatar answered Oct 14 '22 09:10

Thomas Shields


You may take a look at the setInterval method. For example:

window.setInterval(function() {
    // this will execute on every 5 seconds
}, 5000);
like image 4
Darin Dimitrov Avatar answered Oct 14 '22 10:10

Darin Dimitrov