SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Recursion Issue

    Hi everyone,
    signed up to the forums because i'm having a sh!t of a problem with a javascript function of mine.
    i'm designing a small tree system in javascript and am having trouble recurring a function to walk through the tree.

    here's the function i have at the moment:

    function walkTree (node) {
    var children = nodeChildren(node);
    for (i = 0; i < children.length; i++) {
    child = children[i];
    // does certain actions here
    walkTree(child, onclass);
    }
    }

    this function will not recur.

    now i can guarentee that the nodeChildren function returns a simple array that is a list of nodes under the node it is given. i can also guarentee there are no errors in the actions in the loop.

    i've had to change some of the syntax in this function.. and can't really divulge much more than this due to anal management where i work (this is a work project), however im hoping the issue is in my logic.. as there are no problems anywhere else in the script.

    any help would be appreciated.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume nodeChildren() is defined somewhere.

    Why does the function definition have only argument and yet the call to it has two?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Instead of calling walkTree(child, onclass); call setTimeout('walkTree('+child+','+onclass+');',0); instead. Javascript doesn't like recursive calls but you can get around the restriction using setTimeout.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A method or function literally calling itself can provide a short path to a big memory leak.


Bookmarks

Posting Permissions

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