我有一个for循环,它将一系列的facebook id和帖子发布到他们的墙上,但由于某种原因,它会发布正确的次数......但是所有这些都发布在最后一个id的墙上。
如果我在循环中提醒id是不同的,这就是为什么我对这种行为感到困惑,所以我想知道是否有人能看到任何错误?
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
}
}
答案 0 :(得分:1)
您是否在for循环之外定义了friendID var?
您的代码也缺少右括号。
我会以这种方式重新定义循环并再次测试:
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.
}
希望这有帮助!
答案 1 :(得分:0)
管理解决它只是稍微重构我的代码。
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