faqts : Computers : Programming : Languages : JavaScript : Language Core : Functions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 8 people (75%) answered Yes
Recently 6 of 8 people (75%) answered Yes

Entry

How do I call a function delayed AND pass an object along?

Feb 2nd, 2002 16:53
ralf bokelberg, Techek,


// you can use a anonymous function to achieve this. 
// on the assignment of the function the actual scope is copied
function testFunc(parameterObject){
      alert("testFunc " + typeof(parameterObject) + " " + 
parameterObject.name);
}
pObj = new Object();
pObj.name="willi";
setTimeout(function(){testFunc(pObj)}, 1000);
// this even works with local variables
// the following example shows that myArray 
// lives on, even if initVars is finished
function initVars(){
    var myArray = [1,2,3,4];
    myfunc = function(){alert(myArray)};
}
initVars();
myfunc();