Content changing every 10 seconds how to do this?

Discussion in 'JavaScript' started by Geloo, Aug 21, 2010.

  1. #1
    Hallo i'm very new to javascript and I am familiar with php now i want to create a script that every 10 seconds change different content to display example

    when a user comes to the website the content is : blablslbslb
    after 10 seconds its changes to: jajajdjfjd
    and after 10 seconds to: kakawkqr
    and then it goes back to the first one and keeps repeatings this (content isjust a example:p)

    does anybody knows a tutorial on the web to do this?
     
    Geloo, Aug 21, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Well, you can put a delay on your javascript, put it in a loop, and make it refresh every 10 seconds. But there's a better way of doing this kind of thing, you should check out ajax. ;)
     
    Rainulf, Aug 21, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Where is it that the content will come from? Does it need to display new data in real-time or will it only change occasionally? Will the content come from a database? The best method to use will depend on all these circumstances. If the content is coming from a database and you want it to display new data as it is added then AJAX is what you are looking for. Plenty of tutorials out there. If the content is fixed or does not need to load new data, then you could just use php to write the content to a javascript array and iterate through the items. Like:
    
    <div id='jContent'></div>
    <script type="text/javascript">
    var myContentDiv = document.getElementById("jContent");
    var myContent = new Array();
    var c = 0; 
    var myTimer;
    var delayInMs = 10000; //Ten seconds.
    
    //Write these lines with php if the content is from a database.
    [B]myContent.push("content 1");
    myContent.push("content 2");
    myContent.push("content 3");[/B]
    
    function start() {
        myTimer = setInterval(function() {
            myContentDiv.innerHTML = myContent[c];
            if(c<(myContent.length - 1)) c++; else c=0;
        }, delayInMs);
    }
    
    myContentDiv.onmouseover = function() { clearInterval(myTimer); }
    myContentDiv.onmouseout = function() { start(); }
    
    start()
    </script>
    
    Code (markup):
    This will also pause on mouseover and resume on mouseout.

    Another option is to load all the content into an array of DIV's and rotate the visibility of each div. This would be a much better option if the content is long and contains html etc.
     
    Last edited: Aug 22, 2010
    camjohnson95, Aug 22, 2010 IP
  4. Geloo

    Geloo Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks people this really helped me:)
     
    Geloo, Aug 22, 2010 IP