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 6 of 6

Thread: function error.

  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function error.

    hi again -

    further problems with setTimeout, although it could be something else -

    Code:
    function taller(id, origheight, targheight, margin){
    	if(origheight<targheight){
    	  origheight = origheight + 2;
    	  margin = margin - 1;
    	  document.getElementById(id).style.height = origheight+"px";
    	  document.getElementById(id).style.marginTop = margin+"px";
    	  growtimer[1] = setTimeout("taller('+id+','+origheight+','+targheight+','+margin+')",0.5);
    	}
    }
    is giving me errors.

    if i comment out the set timeout line it causes no errors, and if i comment out the two document lines it again causes no erros. i'm confuddled!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    var growtimer=[];
    
    function taller(id, origheight, targheight, margin){
     if(origheight<targheight){
      origheight = origheight + 2;
      margin = margin - 1;
      document.getElementById(id).style.height = origheight+"px";
      document.getElementById(id).style.marginTop = margin+"px";
    // time is in milliSeconds so 0.5 is not a valid time
      growtimer[1] = setTimeout(function(){ taller(id, origheight, targheight, margin); },10);
     }
    }
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <img id="Img1" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" onclick="taller('Img1', 100, 200, 5);"  />
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    legend - that was actually a typo, i was using 20

    similar problem to my last error.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Your original timeout line should have been like this

    growtimer = setTimeout("taller('"+id+"' ,"+origheight+","+targheight+","+margin+")",10);

    note the sinlge and double quotes around id
    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.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr J
    Your original timeout line should have been like this

    growtimer = setTimeout("taller('"+id+"' ,"+origheight+","+targheight+","+margin+")",10);

    note the sinlge and double quotes around id
    i note them - but i don't understand them!

    out of interest why would you need single and double quotes?

  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You are passing a string to setTimeout:

    var id = x;
    var origheight = 5;
    var targheight= 6;
    var magin = 3;

    taller('x' ,5,6,3)

    You need that wrapped in quotes:

    "taller('x',5,6,3)"

    If you use double quotes inside you get this:

    "taller("x",5,6,3)"

    But you didn't use any quotes and got this:

    "taller(x,5,6,3)"

    Where the value there becomes an undefined variable instead of a string.


  •  

    Posting Permissions

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