tags:

views:

19

answers:

1

Hi, i have a problem with the jquery each method.

My Object

first_obj = {};
first_obj[11] = new Array('http://blub.de', 'hsdfe', 'hsdfe' ); 
first_obj[54] = new Array('http://blub.de', 'sdfe', 'hsdfde' ); 
first_obj[99] = new Array('http://blub.de', 'sdf', 'sdfde' ); 

Get Results and insert it into the object "second_obj_results"

    second_obj_results = {};
    $.each(first_obj, function(i, val)
    {       
      var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
      feed.load(function(result) {
        if (!result.error) {
          var container = document.getElementById("feed");
          for (var i = 0; i < result.feed.entries.length; i++)
          {
             second_obj_results[id] = new Array(''+rsd+'', ''+rtitle+'', ''+fege+'' );    // Create new obj with the results
          }
        }
    )};
   read_new_obj();

and here is my problem

When i say read_new_obj(); ... // dont work

and when i say setTimeout('read_new_obj();',2000); ... // it works

Read the new obj

function read_new_obj()
{

    $.each(second_obj_results, function(i, val)
    {   


    // do something

    });


}       

I think the problem is that the "Get Results" is not finished. But the solution with the Timeout is very bad. It is possible that the "Get Results" need more time than 2 seconds.

How can i say, if "each first_object" ready -> Start the function "read_new_obj" ?

Thanks in advance! Peter

PS: Sorry for my bad english :(

+1  A: 

it looks like the feed.load() takes a callback function as parameter. I guess this callback is fired when data has arrived?
If that is the case, put your read_new_obj() method right into that callback.

feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++)
      {
         second_obj_results[id] = new Array(''+rsd+'', ''+rtitle+'', ''+fege+'' );
      }
      read_new_obj();
    }
)};

I don't know the google ajax api very well, but I'm pretty sure .load() will launch an asynchronous request. So your read_new_obj() is executed before the .load() function has finished.

jAndy
Hi jAndy, it works but the function starts 3 times.
Peter
Peter
@Peter: That should be your desired result. You are looping over the `first_obj` which contains three entrys. Well, but it looks like you don't use that information, pretty confusing. I was expecting you are using the first entry from your `first_obj` array to access that url.
jAndy