【问题标题】:AS3 dispatchEvent in forEachforEach 中的 AS3 dispatchEvent
【发布时间】:2012-04-19 16:13:12
【问题描述】:

我已经填充了一个数组。

我需要获取该数组中的每个项目,对该项目执行一些计算,然后将结果推送到数组中,然后移动到数组中的下一个项目,依此类推。

计算是在一个单独的类中进行的。然后当计算完成时,我会派发一个事件并监听类来完成。

我正在使用 forEach,但我需要暂停 forEach 函数并等待 dispatchEvent 侦听器才能继续,但我似乎无法让它工作。

跟踪似乎表明 forEach 只是在数组中运行并每次都重置计算类。

这是我的代码的概要。我从服务器上的 sql 表中填充数组,然后启动 forEach。

谁能提出解决方案:

  function handleLoadSuccessful(evt:Event):void
  {
        evt.target.dataFormat = URLLoaderDataFormat.TEXT;
        var corrected:String = evt.target.data;
        corrected = corrected.slice(1,corrected.length-1);
        var result:URLVariables = new URLVariables(corrected);
        if (result.errorcode=="0") 
      {
            for (var i:Number=0; i < result.n; i++) 
            {
                liveOrderArray.push(
                {   
                    code:result["ItemCode"+i],
              qty:Number(result["LineQuantity"+i]) - Number(result["DespatchReceiptQuantity"+i])
                })                      
            }       
          liveOrderArray.forEach(allocate);
      } else {
            trace("ERROR IN RUNNING QUERY");
             }
  }        
  function allocate(element:*, index:int, arr:Array):void {                
               trace("code: " + element.code + " qty:" + element.qty );
                allocationbible.profileCode = element.code.substring(0,1);
                allocationbible.finishThk = Number(element.code.substring(1,3));
                allocationbible.longEdgeCode = element.code.substring(3,4);
                allocationbible.backingDetailCode = element.code.substring(4,5);
                allocationbible.coreboardCode = element.code.substring(5,6);
                allocationBible = new allocationbible;
                allocationBible.addEventListener("allocated", updateAllocationQty, false, 0, true);
                trace("*************************************");
            }
function updateAllocationQty (evt:Event):void {                 
                //add result to array                   
                trace(allocationbible.coreboardLongCode);               
            }

【问题讨论】:

    标签: actionscript-3 foreach wait dispatchevent


    【解决方案1】:

    如果您需要暂停执行脚本以等待函数完成,那么调度事件并不是您想要的方式。您想要做的是您调用的函数返回您正在等待的值并且根本不使用事件。

    如果我知道您在 allocationbible 中所做的事情,可能会有所帮助

    【讨论】:

    • element.code 是一个 30 位的产品代码,不同的子字符串等于不同的参数。基于这些参数,我计算了原材料的产量,而分配圣经正在计算这种原材料。除了使用 forEach 之外,还有其他方法吗?我指的是使用 dispatchEvent 来获取计算分配所需的信息的几个 sql 表。
    【解决方案2】:

    您不能暂停 for..each 循环,这在 AS3 中是不可能的。所以你需要重写你的代码。

    UPD: 上次误解了你的问题。要在进一步处理之前等待计算完成,您可以在分配事件处理程序中开始处理下一个元素。像这样的:

     var currentItemIndex:int;
    
     function startProcessing():void {
          // population of the array
          // ...
          allocationBible = new allocationbible();
          allocationBible.addEventListener("allocated", onAllocated);
    
          currentItemIndex = 0;
          allocate();
     }
    
     function allocate():void {
          var element:* = liveOrderArray[currentItemIndex];
          // configure allocationBible
          allocationBible.process(element);
     }
    
     function onAllocated(e:Event):void {
          trace("Allocated: " + allocationbible.coreboardLongCode);
    
          // allocate the next item
          currentItemIndex++;
          if (currentItemIndex >= liveOrderArray.length) {
               allocationBible.removeEventListener("allocated", onAllocated);
          } else {
               allocate();
          }
     }
    

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多