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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Unresponsive javascripts

    Why does browser experiences problem of unresponsive javascripts?.Can the problem be resolved by making the code into server-side?
    here my code which becomes unresponsive on multiple clicks.
    [CODE]
    var x=document.getElementsByTagName('td');
    var i,random;
    var colors=["red","blue","green"];
    function randomise()
    {
    random=colors[Math.floor(Math.random()*colors.length)];
    }
    function generate()
    {
    i=0;
    while(i<54)
    {
    randomise();
    if(random=="red")
    {
    x[i].bgColor=random;
    i=i+1;
    }
    else if(random=="blue")
    {
    x[i].bgColor=random;
    i=i+1;
    }
    else if(random=="green")
    {
    x[i].bgColor=random;
    i=i+1;
    }
    }
    }
    [ICODE]
    On a button click the function generate is being called,the script runs fine for the first click but after that it becomes unresponsive,whats the best solution Can writing the same code in php or any server-side language help me ?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You can write the code more simply, and omit globals, which may at least help you figure out the problem.

    Code:
    generate(){
    	var x= document.getElementsByTagName('td'), i=x.length;
    	while(i){
    		x[--i].style.backgroundColor=  
    		["red", "blue", "green"][Math.floor(Math.random()*3)];
    	}
    }


  •  

    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
    •