Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2003, 11:02 PM   PM User | #1
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
array parameters with setTimeout

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
Beck is offline   Reply With Quote
Old 04-01-2003, 11:34 PM   PM User | #2
cheesebagpipe
Regular Coder

 
Join Date: Nov 2002
Posts: 596
Thanks: 0
Thanked 0 Times in 0 Posts
cheesebagpipe is an unknown quantity at this point
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">

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;
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..
cheesebagpipe is offline   Reply With Quote
Old 04-02-2003, 02:00 AM   PM User | #3
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
You are undefined!!!

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
Beck is offline   Reply With Quote
Old 04-02-2003, 02:17 AM   PM User | #4
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Maybe use a class to do your timeouts
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 04-02-2003, 11:27 PM   PM User | #5
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
Great minds think alike

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
Beck is offline   Reply With Quote
Old 04-03-2003, 08:16 PM   PM User | #6
Tails
Regular Coder

 
Join Date: Nov 2002
Posts: 672
Thanks: 1
Thanked 1 Time in 1 Post
Tails is an unknown quantity at this point
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?
Tails is offline   Reply With Quote
Old 04-03-2003, 11:43 PM   PM User | #7
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
arrayed parameters? You mean, can you send an array as a parameter?
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 04-05-2003, 06:03 PM   PM User | #8
Tails
Regular Coder

 
Join Date: Nov 2002
Posts: 672
Thanks: 1
Thanked 1 Time in 1 Post
Tails is an unknown quantity at this point
Often when I put an object in an array, it is retrieved as a string.
There's a .toString() method, but no .toObject(), why?
Tails is offline   Reply With Quote
Old 04-05-2003, 06:12 PM   PM User | #9
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
That doesn't sound right. I'd have to see how you are 'putting objects into arrays' to see what's going on.
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 04-05-2003, 06:20 PM   PM User | #10
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Because they already are objects.
Code:
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.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-05-2003 at 06:26 PM..
liorean is offline   Reply With Quote
Old 04-06-2003, 12:07 AM   PM User | #11
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Dang, liorean is an OOP master.

*bows down*

__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 04-06-2003, 01:13 AM   PM User | #12
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally posted by whammy
Dang, liorean is an OOP master.
<modesty>Yeah. I know.</modesty>
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-06-2003, 01:14 AM   PM User | #13
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
I don't think that statement is allowable in between modesty tags.
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 04-06-2003, 08:21 AM   PM User | #14
Beck
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Beck is an unknown quantity at this point
lucky

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>.
__________________
If at first you don't succeed, spend more time online researching javascript!
Beck
Beck is offline   Reply With Quote
Old 04-06-2003, 11:48 AM   PM User | #15
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:16 PM.


Advertisement
Log in to turn off these ads.