Jquery - Start function when each is ready
i have a problem with 开发者_JS百科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 :(
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.
精彩评论