0

I'm trying to get my jQuery to work in this flow:

  1. Parse json using $getJson
  2. Loop through returned JSON and create divs for each JSON item
  3. Once done, repeat but instead with another json file and append the results to the existing results.

But as of now I can't call another function AFTER the first loop is done, because the function gets called before the page is populated. How can I finish the populating loop, and then call another function?

I appreciate any help you can give me. Still learning as you can tell.

function test() {
  var i;
  for(i=0;i<=10;i++) {
    $("div#test").append("<div id='"+i+"'></div>");
  }
  when_loop_is_done();
}
function when_loop_is_done() {
  var i;
  for(i=0;i<=10;i++) {
    $("div#test div#"+i).append("<span>foo</span>");
  }
}

Essentially I'm grabbing JSON from a separate page on my server, then looping through and populating the content using variables from that JSON object. The issue is no matter what I do, the second function always gets called before jQuery populates the content. So if I were to call an alert after the loop is done the page would pop up the alert and then load in all of the appended html.

1
  • Edit your post to add a sample of your code. It will make it easier to offer advice. Commented Oct 21, 2011 at 6:04

2 Answers 2

1

Store your results in a variable or property. Then, use $(document).ready(fn) to wait for the page to finish loading and populate the page or finish your work when it has finished loading.

Sign up to request clarification or add additional context in comments.

3 Comments

So have a document ready function and detect weather or not the loop has finished? And if it has call the other function?
If you know the JSON will be parsed before pageload everytime, then jfriend00 was just saying you should store those results in a variable, and then append to the divs in the ready fn. So, your entire test function should be inside ready with the assumption that the JSON is ready after page completes loading.
So basically parse json and then call the document ready fn. And in the ready fn is where all of the data will be appended? It doesn't work; getting a 'cannot read property of undefined' when trying to read data[0].test from inside the ready fn. So I'm guessing the JSON isn't being parsed before the page is loaded.
0

Your code should look something like this:

   $.getJSON('/remote.json', function (response) {
     // do the first loop using the response json
   })
   .done(function () {
     // do the second json request and loop through the results
   });

Any code you call inside the done() method's callback function will wait until the previous method in the chain is complete. You should definitely review the API documentation for $.getJSON: https://api.jquery.com/jquery.getjson/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.