...

View Full Version : Problem invoking a method from setInterval/setTimeout



NickH
06-10-2008, 05:40 PM
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?

mrhoo
06-10-2008, 05:57 PM
setInterval(a.b(), 1000);
without the wrapper, the first parameter is interpeted to the return value of the function.

liorean
06-10-2008, 09:12 PM
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
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum