2012-04-19 62 views
0

我已经填充了一个数组。AS3 dispatchEvent in forEach

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

计算是在一个单独的类中进行的。然后当计算完成时,我派遣一个事件并听课以完成。

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

跟踪似乎表明,forEach只是运行数组并重新设置计算类。

这是我的代码jist。我从我的服务器上的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);    
      } 

回答

0

如果需要停止脚本的执行等待功能完成,然后指派事件是不是你想这样做。你想要做的是你调用的函数返回你正在等待的值,根本不使用事件。

或许可以帮助更多的,如果我知道你在allocationbible

+0

element.code是一个30位商品代码不同的子串等于不同的参数。根据这些参数,我计算出原材料的收益率,这是可分配计算的原材料。有没有其他途径比使用forEach?我指的是使用dispatchEvent获取所需信息来计算分配的几个sql表。 – user1344454 2012-04-19 17:13:32

0

在做什么,你不能暂停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(); 
     } 
}