View Full Version : array parameters with setTimeout

04-02-2003, 12:02 AM
How, if possible, do I send an array as a parameter to a function being called by the setTimeout function?

Below is my testing module, and I call collect () (get it? Call collect? haha... sorry... *ahem*) from the onLoad function just to get it running. The functionality is to set up an array and then just pass it to holdArray(). But the alert in holdArray is giving me three undefined values. Should I toss in brackets?

function collect () {
var myArray = new Array (3);
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

function holdArray (theArray) {
alert(theArray[0]+" "+theArray[1]+" "+theArray[2]);

I really want to keep it generic, as in, I'm hoping for the ability to pass any size array I want in, so I cannot just pass the parameters.

Thanks for any feedback :thumbsup:

Of course, if anyone tells me this can't be done, I'll just call you undefined :D

04-02-2003, 12:34 AM
Arrays are objects - timer strings are, well, strings; the only way to store an object reference as a string is in the form of a literal reference (pointer) to the object:

setTimeout('document.forms[0].reset()', 100);

...for example. You can't pass a passed object reference as a string (eval() goes the other way) so you need to store the reference in a persistent (non-local) property somewhere; some people use global variables for this, but since calling timers is usually function-related, I prefer using a function object itself. Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script type="text/javascript" language="javascript">

Array.prototype.toString = function(asHTML) {
for (var sRet='',i=0; i<this.length; ++i)
sRet += '[' + i + '] ---> ' + this[i] + ' : ' + typeof this[i] + ((asHTML) ? '<br />' : '\n');
return sRet;

function collect() {
collect.theArray = new Array (3);
collect.theArray[0] = 1;
collect.theArray[1] = 2;
collect.theArray[2] = 3;

function holdArray (theArray) {



Two ways of referencing it; static function properties are like any other object properties, persistent between function calls, and 'visible' everywhere. This technique is particularly useful with looping timers (animation). Threw in a quick Array printer for you.

You could, of course, just make the array global and pass its name literally....

04-02-2003, 03:00 AM
Thanks for the infos. I'll see what I can do with all that. It's really what I was hoping NOT to hear, but I guess I can't overstretch the limitations of javascript (hey, at least I'm not trying to pass the array to the server ;) ).

Any other input still appreciated. Otherwise, thanks cheesebagpipe. Okay, I'm outties!

04-02-2003, 03:17 AM
Maybe use a class (http://www.codingforums.com/showthread.php?s=&postid=86721#post86721) to do your timeouts :D

04-03-2003, 12:27 AM
Thanks Beetle - yeah, in the end, I did resort to SOMETHING like that. Even moreso, I took a copy of that link's posting to look at the code posting in that thread. Really good looking stuff. Why can't I think like that??? :D

04-03-2003, 09:16 PM
I once asked about a similar question. Can setInterval and clearInteral have arrayed parameters? But what I don't know is...What's a pointer?

04-04-2003, 12:43 AM
arrayed parameters? You mean, can you send an array as a parameter?

04-05-2003, 07:03 PM
Often when I put an object in an array, it is retrieved as a string.
There's a .toString() method, but no .toObject(), why?

04-05-2003, 07:12 PM
That doesn't sound right. I'd have to see how you are 'putting objects into arrays' to see what's going on.

04-05-2003, 07:20 PM
Because they already are objects.
root children decendants of 2nd level
Global Object Function
StringSo, there's no need to convert any other data type to object, unless it comes from a different language/use a different scripting interface, such as VB, ActiveX, C++ classes, Java classes etc.

Note that this is inheritance hierarchy, not object hierarchy. In the object hierarchy, all host objects are children of the global object - in the case of a browser, that is the window object.

04-06-2003, 01:07 AM
Dang, liorean is an OOP master.

*bows down*


04-06-2003, 03:13 AM
Originally posted by whammy
Dang, liorean is an OOP master.
<modesty>Yeah. I know.</modesty>

04-06-2003, 03:14 AM
I don't think that statement is allowable in between modesty tags. ;)

04-06-2003, 09:21 AM
yeah, you're lucky we're usually dealing with HTML here... I think the more strict XML would've registered an error with that particular markup error.

I mean, <sarcasm>Duh!</sarcasm>. :D

04-06-2003, 12:48 PM
Sorry - if you have a look in the ExpressionML DTD, you find this:

<!ELEMENT modesty (#PCDATA)>

Which means my statement was prefectly legal. However, a wrapping in the irony tag is recommended in the spec.

04-08-2003, 12:39 AM