PDA

View Full Version : setTimeout in function call itself and pass attribute???



thenajsays
06-29-2009, 02:24 AM
im writing a function that with an attribute that includes a timeout that calls itself and i cant call the attribute. help!


function shrinkMe(name)
{
if (h1 == 0)
{
h1 = h1
}
else
{
h1--;
document.getElementById("name").style.marginTop = h1 + "px";
setTimeout("shrinkMe(\" + name + "\")", .0001);
}
}

vwphillips
06-29-2009, 09:56 AM
function shrinkMe(name)
{
if (h1 == 0)
{
h1 = h1
}
else
{
h1--;
document.getElementById("name").style.marginTop = h1 + "px";
setTimeout(function(){ shrinkMe(name); }, .0001);
}
}

the minimum practical timeout is 1 milli second

sniperman
06-29-2009, 10:02 AM
I can only warrant a guess as to what you are trying to do in this script but the problem seems to lie on your timeout method.




setTimeout("shrinkMe(\" + name + "\")", .0001);




First off, you must use single ' and double " quotation marks to open and close separate values.


setTimeout("shrinkMe(name);", .0001);

This is semantic code to call a function. If you need to nest a second expression within this expression, you need to use ' ' single quotations. The logic is the same as nesting HTML elements within other elements and closing them off.

Also, you have 5 quotations marks, one too many.

sniperman
06-29-2009, 10:35 AM
Here is a workaround script that shows you how the setTimeout invoke function loop could work and build from there.


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Dynamic Drive Scripts</title>

<script type="text/javascript">
h1=5;
onload = function shrinkMe(name)

{


if (h1 == 0)
{alert("The IF condition is true");
h1 = h1;
}
else
{
h1 = h1-1;
alert("The H1 value in the ELSE statement is " + h1 );
setTimeout("shrinkMe(name);", 1000);
}
}


</script>

</head><body>
<h2> find more tutorials @ <a href="http://www.madchaos.com.au/index.html">www.madchaos.com.au</a></h2>
</body></html>