/ Published in: JavaScript
                    
                                        
With setTimeOut a reference to a function object cannot provide parameters for the scheduled execution of that function.
We can instead use a closure to provide parameters for the execution of a function prior to the execution of that function.
                We can instead use a closure to provide parameters for the execution of a function prior to the execution of that function.
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
function callLater(paramA, paramB, paramC){
/* Return a reference to an anonymous inner function created
with a function expression:-
*/
return (function(){
/* This inner function is to be executed with - setTimeout
- and when it is executed it can read, and act upon, the
parameters passed to the outer function:-
*/
paramA[paramB] = paramC;
});
}
...
/* Call the function that will return a reference to the inner function
object created in its execution context. Passing the parameters that
the inner function will use when it is eventually executed as
arguments to the outer function. The returned reference to the inner
function object is assigned to a local variable:-
*/
var functRef = callLater(elStyle, "display", "none");
/* Call the setTimeout function, passing the reference to the inner
function assigned to the - functRef - variable as the first argument:-
*/
hideMenu=setTimeout(functRef, 500);
URL: http://jibbering.com/faq/notes/closures/
Comments
 Subscribe to comments
                    Subscribe to comments
                
                