javascript - function call with Json-Object, wich contains a function. how to get return variable outside the function? -
i use jfeed plugin read information rss-feed in javascript. can use plugin this:
jquery.getfeed({ url: 'rss.xml', success: function(feed) { alert(feed.title); } });
no want make function, returns feed information this:
function getfeed(feedurl) { message ="error"; jquery.getfeed({ url: 'http://www.spiegel.de/schlagzeilen/tops/index.rss', success: function(feed) { message = feed; } }); return message; }
it not work. how can feed varibale outside scope of jquery.getfeed(...)
?
it wont work way, since method jquery.getfeed
asynchronous , return called earlier.
you need use callback feeds, this:
function getfeed(feedurl, callback){ message ="error"; jquery.getfeed({ url: 'http://www.spiegel.de/schlagzeilen/tops/index.rss', success: function(feed) { callback(feed); } }); } getfeed(feedurl, function(feeds_returned) { console.log(feeds_returned); });
Comments
Post a Comment