Using local objects inside setTimeout

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Using local objects inside setTimeout

Post by alex.barylski »

I'm sure I've done this before, but I can't seem to repeat my previous solutions...

I have an object created inside a function scope and inside that function I call setTimeout()

I need to access that object inside setTimeout()

Any ideas???
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Any variables used in a function which are not explicitly defined as var are assumed to belong to an outer scope, possibly to the Global (window) Object. Are you using "var object = new Object()" or similar?

If all else fails, attach it to window directly: "window.myObject = new Object()"
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Kieran Huggins wrote:Any variables used in a function which are not explicitly defined as var are assumed to belong to an outer scope, possibly to the Global (window) Object. Are you using "var object = new Object()" or similar?

If all else fails, attach it to window directly: "window.myObject = new Object()"
Oh really? Cool...so if I don't use var it's assumed global or at least outside the scope of the function which invokes setTimeout...cool I did not know that...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

one of the (potentially aggravating) subtleties of javascript :-)

Working now?
Post Reply