SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question more setTimeout woes

    Just another quick question:

    Code:
    function calc(value, rate)
      {
      setTimeout("calc(value, rate)", 1000);
      }
    In FireFox error console it says that "value" and "rate" are not defined. But they are?

    Anyone?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function calc(value, rate)  {
      setTimeout(function(){calc(value, rate)}, 1000);
    }
    But what does it matter- all this function does is call a timeout every second. It doesn't use the arguments.

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think thats the way to do it. I don't want to define a whole new function, I want the same function to be called with the same parameters (value and rate).

    So that calc(5, 3)
    would do setTimeout("calc(5, 3)", 1000);

    Of course this function doesn't do anything, I didn't post the calculations it performs because that would just overcomplicate things.

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: Nevermind I see what you meant. Read the "Usefull tricks and tips" sticky, some GOOD stuff in there. For anyone else who is wondering, this is directly from that thread:


    The hidden power of setTimeout
    For some reason, many, many, MANYpeople don't know that the first argument for the setTimeout (and setInterval) function doesn't have to be a string. In fact, it is much more powerful when you send a function pointer, or, a reference to a function (passing it by name), instead.

    So, instead of

    setTimeout( "loop()", 1000 );

    do it this way

    setTimeout( loop, 1000 );

    Now, if we get creative, we can solve the issue of passing arguments to a setTimeout-called function, which I've seen many times. As I'm sure some of you know, the variables you are trying to pass will lose scope (unless they are global). The method to our madness will be the anonymous function.

    Once again, instead of

    setTimeout( "loop(counter)", 1000 );

    We use

    setTimeout( function() { loop(counter); }, 1000 );

    I know what you're thinking -- "Why can't I use setTimeout( loop(counter), 1000 ) ???". I'll tell you why: because that, my friend, is a function call, and if you remember, I said we need to pass a function pointer. Since we declare an anonymous function that itself calls loop(), then we are legitimately passing the proper reference, AND maintaining the power to call the function. It's like having your cake and eating it too!


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
  •