Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Location
    London
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem invoking a method from setInterval/setTimeout

    I have object a with method b that I want to invoke via setInterval from global scope. The following line works...

    setInterval('a.b()', 1000);

    ... but, if possible, I want to pass a function reference rather than a string to setInterval. With a regular function, I have no problem -- setInterval(doIt, 1000); works just fine. But setInterval seems to treat methods differently.

    I tried the obvious...

    setInterval(a.b, 1000);
    -- and --
    setInterval(a.b(), 1000);

    ... but they do not work, in IE6, Firefox 2, or Opera 9. (I get no error message, but method b is never called.) However, if I put an anonymous function wrapper around the method, as in...

    setInterval(function() {a.b()}, 1000);

    ... everything is fine.

    Has anyone seen this before? Why is the function wrapper needed?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    setInterval(a.b(), 1000);
    without the wrapper, the first parameter is interpeted to the return value of the function.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by NickH View Post
    I have object a with method b that I want to invoke via setInterval from global scope. The following line works...

    setInterval('a.b()', 1000);
    This is equivalent to doing setInterval(new Function('a.b()'), 1000);
    ... but, if possible, I want to pass a function reference rather than a string to setInterval. With a regular function, I have no problem -- setInterval(doIt, 1000); works just fine. But setInterval seems to treat methods differently.
    Actually, it doesn't. That it treats them the exact same is in fact trhe source of your problem. You see, if you do this
    Code:
    var
        a=
            {
                b:
                    function(){
                        /*...*/
                    }
            },
        c=a.b;
    c();
    The same function would be called through the use of c(); as if you used a.b();. However, the this object of the c(); call is the global object, while the this object of the a.b(); call is the a object.

    This is because the this object is determined at the time of the call, you see, and is not bound to the function object. In other words it's only the z.x(); syntax for a call that makes it a method call, the call being a method call has nothing to do with whether x is a method of z or not.
    I tried the obvious...

    setInterval(a.b, 1000);
    Which extracts the function object b from the object a and sends that function object to the setInterval function.
    -- and --
    setInterval(a.b(), 1000);
    Which does a method call on a.b and then gives the return value of that method call to the setInterval function.
    ... but they do not work, in IE6, Firefox 2, or Opera 9. (I get no error message, but method b is never called.) However, if I put an anonymous function wrapper around the method, as in...
    Actually, the former should work in all mentioned browsers, just with the this object being the global object instead of a. Whether and how the latter works or not depends on the return value of a.b.
    setInterval(function() {a.b()}, 1000);

    ... everything is fine.

    Has anyone seen this before? Why is the function wrapper needed?
    Because setInterval takes a function object and performs a function call every so often. The only way you can give that function a this object other than the global object is through making a method call yourself, or by not relying on any given this object at all.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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