Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1271 How can I pass an object to a function invoked by a setTimeout, without losing the contents of the array when it goes out of scope?

You are here: irt.org | FAQ | JavaScript | Object | Q1271 [ previous next ]

Use an enabler function (in this case functionB) to return a string made up of the actual values of the array, to be used as the string to be evaluated once the timer expires:

<script language="JavaScript"><!--
function functionA() {
    var parms = new Array('Hello','Cruel','World');
    setTimeout(functionB(parms),7000);
}

function functionB(parms) {
    var s = 'functionC("';
    for (var i=0; i<parms.length; i++) s += parms[i] + ' ';
    return s +'")';
}

function functionC(string) {
    alert(string);
}

functionA();
//--></script>

©2018 Martin Webb