0

I've populated an array.

i need to take each item in that array , perform some calculations on that item push the result to the array then move on to the next item in the array and so on.

The calculations are made in a separate class. Then when the calculations are completed i dispatch an event and listen for the class to complete.

I'm using forEach but i need to pause the forEach function and wait for the dispatchEvent listener before continuing but i can't seem to get it working.

Tracing seems to suggest that the forEach is just running through the array and resetting the calculation class each time.

Here is the jist of my code. I populate the array from a sql table on my server then initiate forEach.

can anyone suggest a solution please:

  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);               
            }
Bali C
  • 30,582
  • 35
  • 123
  • 152

2 Answers2

0

If you need to halt execution of a script to wait for a function to finish then dispatching events is not how you want to do it. What you want to do is the function you call return the value you are waiting for and do not use events at all.

Could probably help more if I knew what you were doing in allocationbible

The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • element.code is a 30 digit product code different substrings equal different parameters. Based on those parameters i calculate a yield for raw material and it is this raw material the allocationbible is calculating. Is there an alternative route than using forEach? I'm referring to several sql tables using dispatchEvent to obtain the required info to calculate allocations. – user1344454 Apr 19 '12 at 17:13
0

You cannot pause for..each loop, it is not possible in AS3. So you need to rewrite your code.

UPD: misunderstood your question last time. To wait upon the completion of calculations before further processing you can start processing of the next element in the allocation event handler. Something like this:

 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();
      }
 }
skozin
  • 3,789
  • 2
  • 21
  • 24