Results 1 to 4 of 4

Thread: setTimeout in function call itself and pass attribute???

  1. #1
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default setTimeout in function call itself and pass attribute???

    im writing a function that with an attribute that includes a timeout that calls itself and i cant call the attribute. help!
    Code:
    	function shrinkMe(name)
    	{	
    		if (h1 == 0)
    		{
    			h1 = h1
    		}
    		else
    		{
    			h1--;
    			document.getElementById("name").style.marginTop = h1 + "px";
    			setTimeout("shrinkMe(\" + name + "\")", .0001);
    		}
    	}

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    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

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    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.

    Code:
    			setTimeout("shrinkMe(\" + name + "\")", .0001);
    First off, you must use single ' and double " quotation marks to open and close separate values.

    Code:
    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.
    Last edited by sniperman; 06-29-2009 at 10:26 AM.

  4. #4
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default a workaround

    Here is a workaround script that shows you how the setTimeout invoke function loop could work and build from there.

    Code:
    <!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>

Bookmarks

Posting Permissions

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