www.webdeveloper.com
Results 1 to 4 of 4

Thread: pop ?

  1. #1
    Join Date
    Nov 2005
    Location
    Bundanoon: first village to ban bottled water
    Posts
    214

    pop ?

    function loadAll(){

    var all = [l1, l2, l3, l4, // l16 ],
    randomnumber = Math.floor(Math.random() * all.length);
    all[randomnumber]();
    }

    This function randomly selects a function to run (each function ajax-loads an image background).

    How to ensure that you don't see the same image twice: that the function selected is eliminated subsequent runs. Maybe it can be done using 'pop', but I need a hand with the syntax… Any helpers?

  2. #2
    Join Date
    Dec 2010
    Posts
    207
    Quote Originally Posted by ctoz View Post
    function loadAll(){

    var all = [l1, l2, l3, l4, // l16 ],
    randomnumber = Math.floor(Math.random() * all.length);
    all[randomnumber]();
    }

    This function randomly selects a function to run (each function ajax-loads an image background).

    How to ensure that you don't see the same image twice: that the function selected is eliminated subsequent runs. Maybe it can be done using 'pop', but I need a hand with the syntax… Any helpers?
    pop is unsuitable because it can only remove the last element.

    Code:
    var all = [ l1, l2, l3, l4 ];
    
    function loadAll()
    {
      if( all.length )
      {    
        all[ randomnumber = Math.floor( Math.random() * all.length ) ]();
        all.splice( randomnumber, 1 );
      }
    }


    Q: I found this code...
    A: Then find its author.

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    You could use a session cookie to indicate which was the last one to load, then remove it from the array.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  4. #4
    Join Date
    Nov 2005
    Location
    Bundanoon: first village to ban bottled water
    Posts
    214
    Thanks, both.

    I think I'll go the splice way: not familiar with cookies.

    @ clueful
    I'd had a look at 'splice', but the way I was thinking about it, it looked too complicated.
    Q
    A
    —and if I get help I like to give credit.

    Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles