Reputation: 11052
I have a for loop that takes an array of facebook id's and posts to their wall but for some reason it posts the right number of times... but all to the last id's wall.
If I alert the id's in the loop they are different which is why I'm confused by this behaviour, so I was wondering if anyone can see any bugs?
JS
for(var z=0; z<friendList.length; z++) {
friendID = friendList[z];
alert(friendID); // this is unique!
FB.api('/' + friendID + '/feed', 'post', options, function(response)
{
// post stuff
}
}
Upvotes: 0
Views: 80
Reputation: 11052
Managed to solve it just be re-structuring my code slightly.
var friendID;
for(var z=0; z<friendList.length; z++) {
friendID = friendList[z];
// this is in the success function of an ajax request previously but I removed it from there
FB.api('/' + friendID + '/feed', 'post', options, function(response)
{
if (!response || response.error)
{
alert('There was an error posting to your friend\'s wall, please refresh the page and try again');
}
else
{
// alert('Success - Post ID: ' + response.id);
}
});
} // end for
// update div with html (this was previously in the else above
Upvotes: 0
Reputation: 23
Are you defining the friendID var outside of the for loop?
Your code is also missing the closing parenthesis.
I'd redefine the loop this way and test again:
var friendID; // friendID declaration.
for (var z = 0, len = friendList.length; z < len; z++) {
friendID = friendList[z];
alert(friendID); // this is unique!
FB.api('/' + friendID + '/feed', 'post', options, function(response) {
// post stuff
}); // missing closing parenthesis.
}
Hope this helps!
Upvotes: 1