...

View Full Version : function error.



limpsharp
07-02-2006, 09:38 AM
hi again -

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


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!

vwphillips
07-02-2006, 09:53 AM
<!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>

limpsharp
07-02-2006, 10:06 AM
legend - that was actually a typo, i was using 20 ;)

similar problem to my last error.

Mr J
07-02-2006, 10:07 AM
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

limpsharp
07-02-2006, 10:57 AM
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?

Beagle
07-03-2006, 04:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum