[thelist] advanced JS question - object scope issue perhaps

Sam Foster sam at sam-i-am.com
Wed Apr 20 09:53:52 CDT 2005


hi Tom,
I didn't see any reply to this, and I'm not sure if you got it already.. 
but here's a couple of thoughts.

>function DynamicHelp(nDelay, sContainerID, sClassName) 
>{
>  
>
..

>	var oContainer = document.getElementById(sContainerID);
>  
>

you've scoped the oContainer variable to the DynamicHelp function.

>function show(){
>	if(nDelay){	
>		oTimer = setTimeout(function(){oContainer.style.display = "block";}, nDelay);
>  
>
Show is called as an object method, but when the setTimeout evals the 
anonymous function, oContainer is out of scope. You need to assign 
oContainer as a property of your object, and refer to that object by 
name (not 'this'.. this is also out of scope when the setTimeout evals).

... I think. Does that help, did you arrive at the same place?

Sam


More information about the thelist mailing list