0

配列を作成しました。

その配列内の各アイテムを取得し、そのアイテムに対していくつかの計算を実行し、結果を配列にプッシュしてから、配列内の次のアイテムに移動する必要があります。

計算は別のクラスで行われます。次に、計算が完了したら、イベントをディスパッチし、クラスが完了するのをリッスンします。

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);               
            }
4

2 に答える 2

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();
      }
 }
于 2012-04-19T22:25:21.737 に答える
0

関数が終了するのを待つためにスクリプトの実行を停止する必要がある場合、イベントをディスパッチする方法は望ましくありません。あなたがしたいことは、あなたが待っている値を返す関数を呼び出し、イベントをまったく使用しないことです。

あなたがallocationbibleで何をしていたかを知っていれば、おそらくもっと役立つでしょう

于 2012-04-19T16:40:40.670 に答える