Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change contents of a div based on timer

Tags:

jquery

Is there any way in jquery to change the contents of a div based on timer.

Assume i have module to give "tips". The contents of the tips should change every 5 seconds.

Thanks

like image 986
kobe Avatar asked Dec 06 '22 01:12

kobe


1 Answers

Make an array of tips. Then make a interval of 5 seconds that change the div's content. I assume you want random tips.

See this example on jsFiddle.

var tips = [
    "Tip 01",
    "Tip 02",
    "Tip 03",
    "Tip 04",
    "Tip 05",
    "Tip 06",
    "Tip 07",
    "Tip 08",
    "Tip 09"
];

// get a random index, get the value from array and
// change the div content
setInterval(function() {
    var i = Math.round((Math.random()) * tips.length);
    if (i == tips.length) --i;
    $("#tip").html(tips[i]);
}, 5 * 1000);

See:

  • setInterval
  • jQuery selectors
  • jQuery html
like image 175
BrunoLM Avatar answered Dec 14 '22 12:12

BrunoLM