Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Object | Q1271 [ previous next ]

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?

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.