需要一些帮助将外循环计数器与dialog.onconfirm()同步

发布于 2024-08-08 10:59:48 字数 2283 浏览 3 评论 0原文

我正在为 Facebook 编写一款游戏。在下面的代码中,我遇到了问题。我有一个 for 循环正在执行,在该循环中,我调用一个对话框并为该对话框实现“onconfirm”。问题是我需要访问 onconfirm 函数内部的循环计数器。但由于 onconfirm 是在 for 循环范围之外调用的,因此计数器值不再有效,因为它已增加。我需要某种方法将计数器值传递给对话框 onconfirm,因为它是在显示对话框时而不是在循环完成之后。或者也许有人有更好的解决方案。任何帮助将不胜感激。谢谢。

function unloadCargo() {
//debugger;
  var actionPrompt = document.getElementById('action-prompt');
  actionPrompt.setTextValue('Unloading cargo...');

  var ajax = new Ajax();
  ajax.responseType = Ajax.JSON;
  ajax.ondone = function(data) {
debugger;
    if(data.unloadableCargo.length == 0) {
      loadCargo();
    } else {
//console.log('unloadable cargo='+dump(data.unloadableCargo));
      var i = 0;
      var j = 0;
      var ucCount = data.unloadableCargo.length;
      for(i = 0; i < ucCount; i++) {
        cargoDialog = new Dialog();
        cargoDialog.showChoice('Unload Cargo', 'Unload  ' + data.unloadableCargo[i].goods_name + ' at ' + data.unloadableCargo[i].city_name + ' for ' + data.unloadableCargo[i].payoff + 'M euros?');
        cargoDialog.onconfirm = function() {
//console.log('unloadable cargo onconfirm='+dump(data.unloadableCargo));
          var ajax = new Ajax();
          var param = {"city_id": data.unloadableCargo[i].city_id, "goods_id": data.unloadableCargo[i].goods_id, "payoff": data.unloadableCargo[i].payoff};
          ajax.ondone = function(demandData) {
            var demands = document.getElementById('demands');
            var innerXhtml = '<span>';
            for(var j = 0; j < demandData.demands.length; j++) {
              innerXhtml = innerXhtml + '      <div class="demand-item"><div class="demand-city">' + demandData.demands[j].city + '</div><div class="demand-pay">' + demandData.demands[j].cost + '</div><div class="demand-goods">' + demandData.demands[j].goods + '</div></div>';
            }
            innerXtml = innerXhtml + '    </span>';
            demands.setInnerXHTML(innerXhtml);
            // update balance
            loadCargo();
          }
          ajax.post(baseURL + "/turn/do-unload-cargo", param);
        }
        cargoDialog.oncancel = function() { loadCargo(); }
      }
      //loadCargo();
    }
  }
  ajax.post(baseURL + '/turn/unload-cargo');
}

I am writing a game for Facebook. IN the following code, I have a problem. I have a for loop executing, and in that loop, I call a dialog and implement 'onconfirm' for the dialog. The problem is that I need to access th e loop counter inside of the onconfirm function. But because the onconfirm is called outside of the scope of the for loop, the counter value is no longer valid because it's been incremented. I need some way to pass the counter value to the dialog onconfirm as it was at the time the dialog was displayed, not after the loop has finished. Or maybe someone has a better solution. Any help would be appreciated. Thanks.

function unloadCargo() {
//debugger;
  var actionPrompt = document.getElementById('action-prompt');
  actionPrompt.setTextValue('Unloading cargo...');

  var ajax = new Ajax();
  ajax.responseType = Ajax.JSON;
  ajax.ondone = function(data) {
debugger;
    if(data.unloadableCargo.length == 0) {
      loadCargo();
    } else {
//console.log('unloadable cargo='+dump(data.unloadableCargo));
      var i = 0;
      var j = 0;
      var ucCount = data.unloadableCargo.length;
      for(i = 0; i < ucCount; i++) {
        cargoDialog = new Dialog();
        cargoDialog.showChoice('Unload Cargo', 'Unload  ' + data.unloadableCargo[i].goods_name + ' at ' + data.unloadableCargo[i].city_name + ' for ' + data.unloadableCargo[i].payoff + 'M euros?');
        cargoDialog.onconfirm = function() {
//console.log('unloadable cargo onconfirm='+dump(data.unloadableCargo));
          var ajax = new Ajax();
          var param = {"city_id": data.unloadableCargo[i].city_id, "goods_id": data.unloadableCargo[i].goods_id, "payoff": data.unloadableCargo[i].payoff};
          ajax.ondone = function(demandData) {
            var demands = document.getElementById('demands');
            var innerXhtml = '<span>';
            for(var j = 0; j < demandData.demands.length; j++) {
              innerXhtml = innerXhtml + '      <div class="demand-item"><div class="demand-city">' + demandData.demands[j].city + '</div><div class="demand-pay">' + demandData.demands[j].cost + '</div><div class="demand-goods">' + demandData.demands[j].goods + '</div></div>';
            }
            innerXtml = innerXhtml + '    </span>';
            demands.setInnerXHTML(innerXhtml);
            // update balance
            loadCargo();
          }
          ajax.post(baseURL + "/turn/do-unload-cargo", param);
        }
        cargoDialog.oncancel = function() { loadCargo(); }
      }
      //loadCargo();
    }
  }
  ajax.post(baseURL + '/turn/unload-cargo');
}

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

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

发布评论

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

评论(1

风追烟花雨 2024-08-15 10:59:48

您需要以某种方式将值传递给对话框。

我从未看过 FBJS,但似乎 setContext 可以用于此目的。

试试这个:

cargoDialog = new Dialog().setContext({currentIndex: i});
// showChoice is the same
cargoDialog.onconfirm = function() {
    alert(this.currentIndex); // Here you should be able to get it
}

You need to pass the value to the dialog somehow.

I have never looked at the FBJS, but it seems setContext can be used for that.

Try this:

cargoDialog = new Dialog().setContext({currentIndex: i});
// showChoice is the same
cargoDialog.onconfirm = function() {
    alert(this.currentIndex); // Here you should be able to get it
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文