JSON 对象 XHR 和闭包

发布于 2024-10-07 10:48:53 字数 674 浏览 3 评论 0原文

我有一个由 XHR 填充的 JSON 对象。然后,我需要使用单独的 XHR 调用中的值更新该对象。我遇到的问题是第二次调用没有在正确的时间进行,我认为这是我如何构造对象的问题。这是我所拥有的:

function Proprietary()
{
  var proprietary= this;
  this.Groups = {};
  this.getGroups = function()
  {
    $.getJSON(group_url,   function(data){proprietary.callReturned(data);});

  }


  this.callReturned = function(data)
  {
   //Do stuff
   for(var i=0; i< data.groups.length; i++)
   {
    insparq.Groups[i] = data.groups[i];
    $.getJSON(participant_url, function(p){proprietary.Groups[i].participants = p;});
    }

//the function call below is the action I want to occur after the object is populated. 
   PopulateGroups();
  }

};

I have a JSON object that is populated by an XHR. I then need to update that object with values from a separate XHR call. The issue I am running into is the second call isn't being made at the correct time and I think it's an issue with how I structured my object. Here is what I have:

function Proprietary()
{
  var proprietary= this;
  this.Groups = {};
  this.getGroups = function()
  {
    $.getJSON(group_url,   function(data){proprietary.callReturned(data);});

  }


  this.callReturned = function(data)
  {
   //Do stuff
   for(var i=0; i< data.groups.length; i++)
   {
    insparq.Groups[i] = data.groups[i];
    $.getJSON(participant_url, function(p){proprietary.Groups[i].participants = p;});
    }

//the function call below is the action I want to occur after the object is populated. 
   PopulateGroups();
  }

};

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

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

发布评论

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

评论(1

栀子花开つ 2024-10-14 10:48:53

该代码片段中似乎有很多奇怪的东西。

首先, $.getJSON(...) 采用第三个参数,这是您的回调。第二个参数是数据。我认为您正在传递一个函数作为数据参数。您需要在两个地方修复此问题。如果您不需要传递任何内容,只需将第二个参数设置为空对象 {} 即可。

这是在所有 XMLHttpRequest 从服务器返回后执行回调的一种通用方法。

this.callReturned = function(data) {

    var countdown = data.groups.length;
    function callback() {
        if(countdown-- === 1) {
            PopulateGroups();
        }
    }

    for(var i=0; i< data.groups.length; i++)
    {
        insparq.Groups[i] = data.groups[i];
        $.getJSON(participant_url, { /* data to send to server */ }, callback);
    }

}

在此代码片段中,每次从服务器返回 XMLHttpRequest 时都会执行回调函数。最后一个执行 PopulateGroups(),在倒计时从 data.groups.length 减到 1 后。

There seem to be a number of weird things in that code snippet.

First of all, $.getJSON(...) takes a third parameter, which is your callback. The second parameter is data. I think you are passing a function as your data parameter. You will need to fix this in both places. If you don't need to pass anything, just have your second parameter be an empty object {}.

Here's one general way you can execute a callback after ALL of your XMLHttpRequests have come back from the server.

this.callReturned = function(data) {

    var countdown = data.groups.length;
    function callback() {
        if(countdown-- === 1) {
            PopulateGroups();
        }
    }

    for(var i=0; i< data.groups.length; i++)
    {
        insparq.Groups[i] = data.groups[i];
        $.getJSON(participant_url, { /* data to send to server */ }, callback);
    }

}

In this snippet, the callback function is executed each time you get a XMLHttpRequest back from the server. The last one executes PopulateGroups(), after the countdown descends to 1 from data.groups.length.

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