开发者

how can I wait for results in javascript?

开发者 https://www.devze.com 2023-04-07 10:35 出处:网络
I have a bit of javascript code that is getting loading in some data from an RSS feed. I then need to process that data and then get some more data from another RSS feed, but it is dependant on the r

I have a bit of javascript code that is getting loading in some data from an RSS feed.

I then need to process that data and then get some more data from another RSS feed, but it is dependant on the results of the first feed.

When I run my app, it processes 开发者_开发知识库both feeds at the same time, how can I make the second feed wait for the first to finish?


You can use the setTimeout function which postpones a function call for the number of milliseconds specified as explained at http://www.w3schools.com/js/js_timing.asp


The trick is to treat the loading of the rss feeds like what they are: asynchronous events.

Do something like this:

var success1=false, success2 = false;

function onRssFeed1Success() { //lets pretend this gets called when rss feed 1 is loaded

  success1 = true;
  doStuffDependentOnBothFeeds()
}


function onRssFeed2Success() { //lets pretend this gets called when rss feed 2 is loaded

  success2 = true;
  doStuffDependentOnBothFeeds()
}


function doStuffDependentOnBothFeeds() {

 if(success1 && success2) {
   //do stuff
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消