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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question setTimeout and getElementById using random array element?

    I have a script that rotates the background of table cells, but I would prefer to do it randomly so it won't matter how many cells I have. However I get the error:

    'document.getElementById(...)' is null or not an object

    from the following code which is supposed to randomly choose the cell from an array of 4:


    Code:
    //rotate the cell background through each cell at a constant rate.
    var delay1 = 150;
    var increasedelay = delay1;
    var spinsoundtime = 1500;
    var limit = Math.round(spinsoundtime/delay1);
    var loop = 1;
       while (loop <= limit & delay1 < spinsoundtime){
    
    	var r_cell = new Array ();
          r_cell[0] = "gb1";
          r_cell[1] = "tv1";
          r_cell[2] = "celp";
          r_cell[3] = "celv";
          var i = Math.round(3*Math.random());
       setTimeout("document.getElementById('r_cell[i]').className='greybg'",delay1);
    	delay1 = delay1 + increasedelay;	
    	loop++;
       }//end while
    If I take the quotes of 'r_cell[i]' --> r_cell[i], I get a new error:
    r_cell is undefined

    Can I not pass a variable this way? Is the array undefined for some reason because it is in a while loop, does it have to be initialized or something?

    Not very familiar with javascript syntax.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    At a glance: it may be because the script is looking for an element object which has not yet been created; see if placing the code block into the body--below the elements in question--resolves the error.
    *this message will self destruct in n-seconds*

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    It could be that because you have used Math.round at some stage var i might equal 4 of which you do not have r_cell[4]

    Try math.floor()

    var i = Math.floor(3*Math.random());

    Math.round()
    If the fractional portion of number is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number is less than .5, the argument is rounded to the next lowest integer.


    Also should this line

    while (loop <= limit & delay1 < spinsoundtime){

    be like this

    while (loop <= limit && delay1 < spinsoundtime){
    Last edited by Mr J; 01-08-2005 at 08:03 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    New Coder
    Join Date
    May 2003
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    "a" solution

    You may be right, I put the variable at the end of the object and all seemed fine, then I just decided to do it this way. A little more long winded, but it works:

    Code:
    function RotateCellsOnesong(){
    //rotate the cell background through each cell at a constant rate.
    var delay1 = 150;
    var increasedelay = delay1;
    var spinsoundtime = 1500;
    var limit = Math.round(spinsoundtime/delay1);
    var loop = 1;
       while (loop <= limit & delay1 < spinsoundtime){
    
    	var r_cell = new Array ();
          r_cell[0] = "gb1";
          r_cell[1] = "tv1";
    		r_cell[2] = "celp";
    		r_cell[3] = "celv";
          var i = Math.round(3*Math.random());
     	if (r_cell[i] == "gb1"){
       setTimeout("document.getElementById('gb1').className='greybg'",delay1);
       setTimeout("document.getElementById('tv1').className='stats'",delay1);
       setTimeout("document.getElementById('celp').className='stats'",delay1);
       setTimeout("document.getElementById('celv').className='stats'",delay1);
     	}
     	if (r_cell[i] == "tv1"){
       setTimeout("document.getElementById('gb1').className='stats'",delay1);
       setTimeout("document.getElementById('tv1').className='greybg'",delay1);
       setTimeout("document.getElementById('celp').className='stats'",delay1);
       setTimeout("document.getElementById('celv').className='stats'",delay1);
     	}
     	if (r_cell[i] == "celp"){
       setTimeout("document.getElementById('gb1').className='stats'",delay1);
       setTimeout("document.getElementById('tv1').className='stats'",delay1);
       setTimeout("document.getElementById('celp').className='greybg'",delay1);
       setTimeout("document.getElementById('celv').className='stats'",delay1);
     	}
     	if (r_cell[i] == "celv"){
       setTimeout("document.getElementById('gb1').className='stats'",delay1);
       setTimeout("document.getElementById('tv1').className='stats'",delay1);
       setTimeout("document.getElementById('celp').className='stats'",delay1);
       setTimeout("document.getElementById('celv').className='greybg'",delay1);
     	}
    	delay1 = delay1 + increasedelay;	
    	loop++;
       }//end while
    }//end function RotateCellsOnesong


  •  

    Posting Permissions

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