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!

1

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.