Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    sequential array listing loop

    I have a simple rotating text script that writes from the array at random. I am not sure how to make this sequential.
    Code:
    var rannum= Math.floor(Math.random()*textarray.length);
    document.getElementById('ShowText').innerHTML=textarray[rannum];
    where 'textarray' is - well, the text array

    I imagine there will need to be:
    1- an element count
    2- an incrementing counter
    check 2 against 1 > then start again at the beginning.

    I can do this in PHP but my js is a bit shaky..
    Steady my hand someone -

    thanks

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Does this code run when the page is loaded? And if it's loaded again do you want to advance the number? This rannum code will work like that but the incremental code will need to use a cookie or some other way to remember the number from one time to the next. With php you could use a $_SESSION variable to remember it.

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Code:
    var textarray = [ ..array.. ];
    function RndText() {
    var rannum= Math.floor(Math.random()*textarray.length);
    document.getElementById('ShowText').innerHTML=textarray[rannum]; }
    onload = function() { RndText(); }
    var inter = setInterval(function() { RndText(); }, 5000);
    Code:
    <div id="ShowText"></div>
    This runs through the array after a single page load.
    I imagine I need something like:
    Code:
    document.getElementById('ShowText').innerHTML=textarray[INCREMENTINGval];
    I guess the loop works with setInterval
    I have tried things along the line of:
    Code:
    INCREMENTINGval = INCREMENTINGval++;
    if (INCREMENTINGval==textarray.length) {INCREMENTINGval=0; }
    but am unsure...
    if I am thinking correctly,
    where to insert this,
    or... is it not possible and I need to use a session value?

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •