问题描述
我在for循环中运行FB.api时遇到问题.
I am facing a problem with runing FB.api in a for loop.
for(var i = 0; i < commentObjectLength; i++){
var fbFeedID = commentObject.OwnCommentList[i].fbFeedID;
var OwnCommentID = commentObject.OwnCommentList[i].OwnCommentID;
var accessToken = commentObject.OwnCommentList[i].accessToken;
var commentText = commentObject.OwnCommentList[i].commentText;
alert("Hiii"); // >>>>>>>1
FB.api('/' + fbFeedID + '/comments', 'post', {
message: commentText,
access_token : accessToken
}, function (response) {
if (!response || response.error){
//alert(response.error.message);
} else {
alert("Hello"); // >>>>>>>2
alert(response.id); // >>>>>>>3
}
});
}
现在要用简单的方式讲.
我希望能像
1-> Hiii
2->你好
3->一些响应ID
4-> Hiii
5->你好
6->一些共振ID
Now to tell in simple way.
I am expecting out in alert like
1-> Hiii
2-> Hello
3-> some response id
4-> Hiii
5-> Hello
6-> some resonse id
但是实际上我就像
1-> Hiii
2-> Hiii
3-> Hiii
But in reality I am getting out like
1-> Hiii
2-> Hiii
3-> Hiii
一直持续到循环大小
8->你好
9->一些响应ID
10->你好
11->一些响应ID继续
continues till loop size
8-> Hello
9-> some response id
10-> Hello
11-> some response id continues
问题始终是循环的最后日期将转到Facebook并在feed上发表评论.不单独.
Problem is all time the last date of loop will go to Facebook and comment on feed. Not separately.
因此,请任何人帮助我解决此问题.我很努力.
(希望每个人都能理解问题,需要任何其他信息,请询问)
So please any one help me solve this issue. I am struggling a lot.
(Hope every one can understand problem, anything extra info needed please ask)
推荐答案
FB.*是基于事件的,有一些技巧可以使它更加同步,但实际上无法正常工作.您需要链接函数调用.
FB.* is event based there are some tricks to make it more synchronous but it will not work realy. You need to chain your function call.
for(var i = 0; i < commentObjectLength; i++){
var fbFeedID = commentObject.OwnCommentList[i].fbFeedID;
var OwnCommentID = commentObject.OwnCommentList[i].OwnCommentID;
var accessToken = commentObject.OwnCommentList[i].accessToken;
var commentText = commentObject.OwnCommentList[i].commentText;
FB.api('/' + fbFeedID + '/comments','post',{
message: commentText,
access_token : accessToken
}, function (response) {
alert("Hiii"); // >>>>>>>1
if (!response || response.error){
//alert(response.error.message);
} else {
alert("Hello");// >>>>>>>2
alert(response.id); // >>>>>>>3
}
});
}
这篇关于我需要帮助以同步模式进行FB.api()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!