2016-05-15 61 views
1

我有一个可以正常工作的功能,但是如果我增加记录号,就会发生15秒的超时错误。我已经看到暗指将一个函数“分类”,将其分解为大块,以便让处理器再次开始15秒,但似乎无法使其正常工作。 代码:AS3:如何分解函数以避免15秒超时规则?

startBatch=0; 
private function findDupes():void { 
    var el:Number; //elapsed time variable 
    timeoutTime = getTimer(); 
    for (var i:int = startBatch; i < numTix; i++) { // numTix = total number of records 
     for (var j:int = i + 1; j < numTix; j++) { 
      if (individualTicket[i] == individualTicket[j]) { 
       // mark duplicate 
      } 
     } 
     el = getTimer() - timeoutTime; 
     if (el > 1000) { 
      trace("batched out"); 
      batchOut(i); 
      return; 
     } 
    } 
    weAreDone(); 
} 

private function batchOut(i:int):void { 
    updateTB2(i); //attempts to update a textbox and FAILS to do so 
    trace("Out at # ", i); 
    if (i < numTix) { 
     startBatch = i; 
     findDupes(); 
    } 
    else { 
     weAreDone(); 
    } 
} 

因此,每一秒,其 “批出”,并在新的号码(startBatch)开始findDupes()函数了。我曾希望这会重置超时错误,但相反,我得到一根棍子的垃圾。

任何人都可以指向正确的方向吗?

+0

您需要检查内循环中的时间,然后保存外循环的值以处理下一帧的剩余部分。 – Vesper

回答

1

您需要修改batchOut函数。目前,它不允许Flash引擎更新屏幕上的任何内容,因为它会立即调用findDupes的另一次迭代,而应该在下一帧开始时返回并设置另一个迭代的类。我在这里假设这个代码中有一个stage变量可用。你需要一些东西来允许听Event.ENTER_FRAME为了做这种类型的批处理,stage是显示对象的通用锚点。

private function batchOut(i:int):void { 
    updateTB2(i); //attempts to update a textbox and FAILS to do so 
    trace("Out at # ", i); 
    if (i < numTix) { 
     startBatch = i+1; // your findDupes pass the already processed value of outer index 
     // findDupes(); this is a recursion call, drop 
     if (!(stage.hasEventListener(Event.ENTER_FRAME,continueBatch)) { 
      stage.addEventListener(Event.ENTER_FRAME,continueBatch); 
     } 
    } 
    else { 
     weAreDone(); 
     if (stage.hasEventListener(Event.ENTER_FRAME,continueBatch)) { 
      stage.removeEventListener(Event.ENTER_FRAME,continueBatch); 
     } 
    } 
} 
// now the function to be called 
private function continueBatch(e:Event):void { 
    // this is called in the NEXT frame, so you can freely call your worker function 
    findDupes(); 
} 
+0

谢谢你,Vesper!我的名声不会显示我的赞成,但我试过! :)但我确实接受了你的答案。我为你解决的主要问题是,我的代码没有进入下一帧,从而打破了15秒的倒计时,对吧?确切地说, – Chowzen

+0

。需要释放代码流(从任何地方返回)以便Flash进入下一帧。 – Vesper