Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    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?

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    Often when I put an object in an array, it is retrieved as a string.
    There's a .toString() method, but no .toObject(), why?

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 04-05-2003 at 06:26 PM.
    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

  • #11
    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
    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. ;)

  • #12
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #13
    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
    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. ;)

  • #14
    New Coder
    Join Date
    Jun 2002
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #15
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •