Bryan Fajardo
Bryan Fajardo

Reputation: 161

I want to make 3 or more divs show one after the other using Javascript

I have a section on my site I would like to show some alerts or news on. I want to show the first news for x seconds, then move on to the second news for x seconds and not show the first news anymore, and then unto the third and not show the second .. etc. I am just starting with javascript and the code I was making worked only for one run and then stopped I would like this to go on infinitely or a very high number so that the person can see the news alerts while they are on the page. One after the other.

I appreciate any help I can get. Please show me a thorough code because I'm a bit noobish when it comes to this since I started about 2 weeks ago. Thanks!

Html:

    <div id="news1">Here are some news! 1111</div>
    <div id="news2">Here are some news! 2222</div>
    <div id="news3">Here are some news! 3333</div>

CSS:

#news1, #news2, #news3{

    visibility:hidden;
}

Javascript:

    function showIt() {
        document.getElementById("news1").style.visibility = "visible";
        document.getElementById("news3").style.visibility = "hidden";
        document.getElementById("news2").style.visibility = "hidden";

    }
    setInterval("showIt()", 3000); 
    function showIt2(){
        document.getElementById("news2").style.visibility  = "visible";
        document.getElementById("news1").style.visibility = "hidden";
        document.getElementById("news3").style.visibility = "hidden";
    }
    setInterval("showIt2()", 6000)
    function showIt2(){
        document.getElementById("news2").style.visibility  = "visible";
        document.getElementById("news1").style.visibility = "hidden";
        document.getElementById("news3").style.visibility = "hidden";
        setInterval("showIt3()", 3000);
    }

Upvotes: 0

Views: 339

Answers (4)

mickro
mickro

Reputation: 891

First understand what how to use setInterval(). You specify 2 paramaters as function to call and delay which specify how long time to wait between two calls.

Then in your example:

  • setInterval("showIt()", 3000); // will call showIt() each 3000 ms
  • setInterval("showIt2()", 6000); // will call showIt2() each 6000 ms
  • setInterval("showIt3()", 3000); // will call showIt3() each 3000 ms (after waiting 6000ms cause in showIt2())

put that on timeline :

  • 0 ms: nothing
  • 3000 ms: showIt() is called
  • 6000 ms: showIt() is called and showIt2() is called
  • 9000 ms: showIt() is called and showIt3() is called
  • 12000 ms: showIt() is called, showIt2() is called and showIt3() is called
  • 15000 ms: showIt() is called and showIt3() is called twice
  • ...

You should try to work with only one setInterval() and variable which select next function should be called.

var myIndex = 0;

setInterval(updateFunction, 3000);

function updateFunction(){
  switch(myIndex)
  {
    case 0: showIt();
    break;
    case 1: showIt2();
    break;
    case 2: showIt3();
    break;
  }
  ++ myIndex;
  if (myIndex > 2) myIndex = 0;
}

EDIT add to: http://jsfiddle.net/fA4cu/

Upvotes: 1

acdcjunior
acdcjunior

Reputation: 135772

Without changing your CSS/HTML, the code below will handle any number of elements, all you have to do is send their IDs as parameter:

function showAlternate(ids, interval, currentVisible) {
    if (currentVisible === undefined) currentVisible = ids.length-1; // last one
    document.getElementById(ids[currentVisible]).style.visibility = "hidden";
    var idToShow = ++currentVisible % ids.length;
    document.getElementById(ids[idToShow]).style.visibility = "visible";
    setTimeout(function() { showAlternate(ids, interval, idToShow); }, interval);
}
// example usage:
showAlternate(["news1", "news2", "news3", "news4", "news5"], 3000);

Demo jsfiddle here:

Upvotes: 2

Ashis Kumar
Ashis Kumar

Reputation: 6554

I have changed logic of the javascript code like below,

var newsList = ["news1","news2","news3"];
var currIndex = newsList.length-1;
var timeoutObj;
function showNext() {
    document.getElementById(newsList[currIndex]).style.visibility = "hidden";
    if(currIndex >= newsList.length-1)
        currIndex = 0;
    else
        currIndex += 1;
    document.getElementById(newsList[currIndex]).style.visibility = "visible";
    timeoutObj = setTimeout(showNext,3000); 
}
showNext();

Check if it helps you.. Here's the jsfiddle http://jsfiddle.net/m63Up/

Upvotes: 2

sajawikio
sajawikio

Reputation: 1514

This Pure-JS example will work with any number of divs with your current pattern, so you can have id "news4", "news5", "news6", etc.

Also, you can change how frequently the news changes that is shown by changing the INTERVAL variable, in this example the news will change every 1 second.

JSFIDDLE -- http://jsfiddle.net/g9YRb/3/

JS

var INTERVAL = 1000;
var myprefix = "news";
var maxnewsnumber = document.getElementsByTagName("div").length;


var myindex = 0;

function alternatenews(idx)
{
   idx++;

   myindex = idx;
    function retrigger()
    {
        setTimeout(function()
       {
           alternatenews(myindex);
       },INTERVAL);
    }
   if(myindex > maxnewsnumber)
   {
      document.getElementById(myprefix + parseInt(myindex-1)).style.visibility = "hidden";
       myindex = 0;
       alternatenews(myindex);

   }
    else if(myindex == 1)
   {
       document.getElementById(myprefix + myindex).style.visibility = "visible";
       retrigger();
   }
    else
    {
        document.getElementById(myprefix + parseInt(myindex-1) ).style.visibility = "hidden";
       document.getElementById(myprefix + myindex).style.visibility = "visible";
        retrigger();
    }

}
alternatenews(myindex);

HTML

<div id="news1">Here are some news! 1111</div>
<div id="news2">Here are some news! 2222</div>
<div id="news3">Here are some news! 3333</div>
<div id="news4">Here are some news! 4444</div>
<div id="news5">Here are some news! 5555</div>

CSS

div
{
   visibility: hidden
}

Upvotes: 0

Related Questions