Reputation: 2001
for(var p = 0; p < xmls.length; p++) {
var feed = new google.feeds.Feed(xmls[p]);
feed.load(function(result) {
//code
In the above code, if I output p outside of the feed.load function, I correctly see that p iterates. However, if I output p inside that function (where I need to access it), It remains at 2 (which happens to be the length of the array "xmls"). I know this is a scope issue, but not how to fix it.
Thanks in advance!
Upvotes: 1
Views: 432
Reputation: 284816
You need to create a new scope, which means a new function.
for(var p = 0; p < xmls.length; p++) {
(function(p) {
var feed = new google.feeds.Feed(xmls[p]);
feed.load(function(result) {
// use p
});
})(p);
}
Upvotes: 3