Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:22:21+00:00 2026-05-27T23:22:21+00:00

In my ajax application I’m trying to use a recursive function inside of which

  • 0

In my ajax application I’m trying to use a recursive function inside of which another function is called which creates a global variable, and I want to use that variable inside the recursive function. However, I’m running into trouble because I can’t access that global variable inside the recursive function (although it is available if I call that function on its own). The code for the recursive function is below.

ajaxCity.displayCell() sets a window.cells[] variable, which contains an array of numbers. The idea is to call the tree() function for each of those numbers.

function tree (topCell) {
    var ajaxCity = new Ajax();  
    ajaxCity.displayCell(topCell);

    for (var i = 0; i < window.cells[topCell].cKids.length; i++) {
        tree(window.cells[topCell].cKids[i]);
    }
}

The window.cells[] variable is available after I run the function once, but how should I change the process so that it works the way I’m imagining?

Thanks for your help!

edit: here’s the error that I get.

TypeError: window.cells[topCell] is undefined

if I call the tree function again, it will work for one “level” deeper, then give the error again, etc

================

Thank you James Montagne, as you pointed out, the problem is that I need to wait for the ajax request to come back. Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:22:22+00:00Added an answer on May 27, 2026 at 11:22 pm

    I’m making some assumptions here based on variable names, but I take it that

    ajaxCity.displayCell(topCell);
    

    will make an ajax call, the result of which will be the creation of window.cells....

    If this is the case, the problem is that AJAX is asynchronous (That’s the first A). If you make an AJAX request and then immediately try to use the result of that request, you will not find a result. This is because the AJAX request has not yet returned. You will need to instead work with the result in the callback of the AJAX request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an Ajax application which uses a callback function to add a
I have a basic ajax application, which will not work, instead the php code
I have an AJAX application which has used CSS divs to create panels on
We are building an ajax application in which a users input is submitted for
I had a realtivley simple ajax application, which I have broken up to be
I am currently logging an AJAX application with messages which include the times of
I've projected an Intranet Ajax application and I want put it in a full
I am converting a javascript/php/ajax application to use jQuery to ensure compatibility with browsers
I am working on the jsp/servlet/ajax application. I use XMLHttpRequest to pass values from
I'm trying to implement a decent navigation in a AJAX application. What I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.