views:

31

answers:

1
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!

+3  A: 

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);
}
Matthew Flaschen
thanks! That was it.
codersarepeople