使用 JavaScript 通过 for 循环将 FB.api 调用的响应保存到数组

发布于 2024-11-06 09:29:32 字数 678 浏览 0 评论 0原文

我正在尝试编写一个脚本,1)从文本输入字段读取 FB 用户 ID将它们循环到一个数组中 2) 使用名字的每个 ID 调用 FB.api,并将返回的名字值保存到另一个数组中。 问题:由于某种原因,从 FB.api 收到的所有名字都保存到同一个数组索引位置 5。这很奇怪,因为循环应该只从索引 1 到 4。

<script type="text/javascript">
var ids = new Array();
var names = new Array();
var fields = 4;


function firstNames() {

var i;
var k;


for (i=1;i<=fields;i++)
    {
        ids[i] = document.getElementById("field"+i).value;
    }

for (k=1; k<=fields; k++)
    {
        FB.api('/'+ids[k], function(response) {
            names[k] = response.first_name;
            alert(k+' test '+names[k]);
        });
    }

}
</script>

有人遇到过类似的情况吗?任何帮助将不胜感激。

预先感谢。

I'm trying to a code a script that 1) Reads FB user IDs from text input fields & loops them into an array 2) calls FB.api with each of the IDs for first name and saves the returned first name value into another array.
The problem: for some reason, all the first names received from FB.api save to the same array index place 5. Which is extra weird since the loop should only go from indexes 1 to 4.

<script type="text/javascript">
var ids = new Array();
var names = new Array();
var fields = 4;


function firstNames() {

var i;
var k;


for (i=1;i<=fields;i++)
    {
        ids[i] = document.getElementById("field"+i).value;
    }

for (k=1; k<=fields; k++)
    {
        FB.api('/'+ids[k], function(response) {
            names[k] = response.first_name;
            alert(k+' test '+names[k]);
        });
    }

}
</script>

Has anyone ran into something similar? Any help would be much appreciated.

Thanks beforehand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-11-13 09:29:32

阅读:Javascript、node.js 和 for 循环
参见:实验#7:通过建立新变量的新作用域关闭
URL: http: //blog.mixu.net/2011/02/03/javascript-node-js-and-for-loops

我和你有同样的问题,即 FB.api 获取 FOR 循环最终迭代索引...

请尝试此解决方案:

for (k=1; k<=fields; k++)
{
  // Closure with new scope establishing a new variable
  (function() {
    var index = k; // new variable
    FB.api('/'+ids[index], function(response) {
        names[index] = response.first_name;
        alert(index+' test '+names[index]);
    });
  }();
}

更新:需要注意的一件事是,此示例中的警报可能不会按顺序发生,即您可能会得到 1,4,2,3...这完全取决于每个 API 调用的网络延迟与其执行顺序有关...

Read: Javascript, node.js and for loops
See: Experiment #7: Closure with new scope establishing a new variable
URL: http://blog.mixu.net/2011/02/03/javascript-node-js-and-for-loops

I had the same issue as you i.e. FB.api gets the FOR loops final iteration index...

Please try this solution:

for (k=1; k<=fields; k++)
{
  // Closure with new scope establishing a new variable
  (function() {
    var index = k; // new variable
    FB.api('/'+ids[index], function(response) {
        names[index] = response.first_name;
        alert(index+' test '+names[index]);
    });
  }();
}

Update: One thing to note is that the alerts in this example probably won't happen in sequence i.e. you might get 1,4,2,3... it all depends on network latency of each API call as to the order they are executed in ...

半枫 2024-11-13 09:29:32

这不是同一个 Api 调用,但我实际上遇到了类似的情况。

你必须明白 facebook api 调用是异步的。最重要的是,它很慢。因此 javascript 速度更快,并且在发送第一个响应时,您的 javascript for 循环已经完成。这就是为什么所有内容都被发送到 k=5,这是循环后 k 的值。

发生这种情况是因为 javascript 不会像 java 那样强制您在处理程序中使用最终变量。

现在你明白了,如何解决它?呵呵,抱歉,当我遇到这种问题时,我想出了一个肮脏的解决方案。

我所做的是直接在函数(响应)主体中处理代码,而不是使用中间变量。因此,作为快速修复,您可以执行相同的操作并忘记您的名称数组,或者找到更好的东西。

It wasn't the same Api call but i actually ran into something similar.

You have to understand that the facebook api call is asynchronous. And most importantly, it's slow. So javascript is way faster, and by the time the first response is sent, your javascript for loop is already completed. That's why everything is sent into k=5, which is k's value after the loop.

This is happening because javascript doesn't force you to use a final variable in the handler, like java does.

So now you understand, how to fix it ? Heh sorry, i came out with a dirty solution when i had this kind of problem.

What I did was handling the code directly in the function(response) body, and not use intermediary variables. So as a quick fix you can do the same and forget about your names array, or find something better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文