Results 1 to 5 of 5

Thread: Help with variable passing

  1. #1
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with variable passing

    Hi guys,

    I have a total case of mental block today. This function recieves a variable, and then needs to set up a timer which calls another function, sending on that variable to the timered function.

    With me?

    Code:
    function process(which2){
    cleartimer()
    setTimeout("fadeoutest(which2)",50)
    }
    I want it to pass the "which2" variable on to the fadeoutest function, but it doesn't seem to want to; script error messages.

    I could do this:

    Code:
    function process(which2){
    cleartimer()
    var2=which2
    setTimeout("fadeoutest(var2)",50)
    }
    But it doesn't seem like a very good way to do it.

    Is that the only workaround?
    cr3ative
    Last edited by cr3ative; 01-28-2005 at 05:03 PM.
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by cr3ative
    Code:
    function process(which2){
    cleartimer()
    setTimeout("fadeoutest(which2)",50)
    }
    Simple.

    Code:
    function process(which) {
      cleartimer();
      setTimeout(function() {fadeoutest(which);}, 50);
    }
    The function expression passed to setTimeout forms a closure, preventing garbage collection of the outer argument, which. The comp.lang.javascript FAQ covers (in the notes) closures in detail, including what I've presented above regarding the use of references with setTimeout. However, the explanation of the former is very detailed so if you want a gentler introduction into the specifics, just ask.

    Code:
    function process(which2){
    cleartimer()
    var2=which2
    setTimeout("fadeoutest(var2)",50)
    }
    But it doesn't seem like a very good way to do it.
    As the latter article I cited states (I've assumed you've read it now), obsolete browsers don't support references as arguments to setTimeout. In this case, you'd have to provide global access to the function expression. The toString method assigned to the function expression could then call the function object via this global. However, if calls to process can occur in rapid succession, things start becoming more complicated (but still feasible). I won't bother explaining this unless you say it'll be necessary.

    Hope that's some food for thought,
    Mike

  3. #3
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Cheers Mike - this should help a lot in my project.

    cr3ative
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  4. #4
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    lol, this old post just totally helped me.

  5. #5
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Wow, that's a lot of views. Not surprising considering how I found it:

    http://www.google.com/search?q=pass+variable+settimeout

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
  •