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;
window.setTimeout("holdArray("+myArray+");",50);
}
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
Of course, if anyone tells me this can't be done, I'll just call you undefined
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
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">
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
function collect() {
collect.theArray = new Array (3);
collect.theArray[0] = 1;
collect.theArray[1] = 2;
collect.theArray[2] = 3;
setTimeout("holdArray(collect.theArray)",50);
}
function holdArray (theArray) {
alert(theArray);
alert(collect.theArray);
}
collect();
</script>
</head>
<body>
</body>
</html>
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....
Last edited by cheesebagpipe; 04-02-2003 at 01:46 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!
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
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???
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
root children decendants of 2nd level
Array
Boolean
Date
Error
Global Object Function
Math
Number
RegExp
String
So, 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.