Jump to content

recursive functions with setTimeout


chadmichael

Recommended Posts

Hello.I'm trying to invoke a function repeatedly by putting a setTimeout call in my function that calls itself. The problem is that I can't repass my parameters due to the string based nature of the argument to the setTimeout function. I also have found that the setTimeout uses the Window context for its context; otherwise you could expect the function to find the parameterOne still defined in the call stack, which would be adequate. My code is below and if you run it you will see that "parameterOne" is undefined on the second iteration. How do I pass it in?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head><title>Test Page</title><script language="JavaScript">function doSomething( parameterOne ){   alert ( "parameterOne = " + parameterOne );   setTimeout ( "doSomething( parameterOne)", 2000 );}doSomething ( "hello" );</script></head><body>EMTPY HTML BODY</body></html>

Link to comment
Share on other sites

setTimeout executes only once but setInterval (used exactly the same as setTimeout) executes over and over again until you tell ti to stop (really go for constantly checking a page's status or something).

Link to comment
Share on other sites

I was interested in using the setInterval ( func, interval, args ). This one takes a function instead of a string based bit of code to execute. However, I only works once and then throws an error about useless invocation of setInterval due to missing quotes? Does anyone have any experience using the setInterval function with this signature, such as

<script language="javascript"> setInterval ( nextSlide ( slideShow1 ), 2000l ); </script>

Link to comment
Share on other sites

You can pass a global varaiable, maybe this will help.

  var count = 0;  var cmd;  function doSomething( parameterOne ){  alert ( "parameterOne = " + parameterOne );  cmd = parameterOne + count++;  setTimeout( 'doSomething(cmd)', 2000 );}doSomething ( "hello" );

Or pass a literal string.

  var count = 0;  function doSomething( parameterOne ){  alert ( "parameterOne = " + parameterOne );  str = parameterOne + count++;  setTimeout( 'doSomething("' + str + '")', 2000 );}doSomething ( "hello" );

HTH

Link to comment
Share on other sites

The global variable works becuase the context of the function, when invoked by the setTimeout, is the global context. However, this doesn't work for me because I might have multiple instances of this code running in a page. This creates concurrency issues for global variables. The string literal doesn't work for me either becuase, in my real code, I'm passing a more complex object as my parameter. Thanks for the suggestions though.

Link to comment
Share on other sites

How about a global array..that way your calls are distinct.

   var count = 0;   var cmd = new Array();      function doSomething( parameterOne )  {     count++;     alert ( "parameterOne = " + parameterOne );     cmd[count] = parameterOne + count;     if(count > 6) return;     setTimeout( 'doSomething(cmd[' + count +'])', 2000 );  }      doSomething ( "first" );  doSomething ( "next" );  doSomething ( "last" );

Link to comment
Share on other sites

How would setInterval help out?

Unless the setTimeout is changing every time, setInterval seems a better option. You can't keep the object local unfortunately either, due to the nature of the function combined with the setInterval/setTimeout (can you imagine the memory leakage?). A global variable/array as adservio suggested would be a good idea, and easy to keep track of :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...