【问题标题】:AS3: How do I break up a function to avoid the 15 second time out rule?AS3:如何分解函数以避免 15 秒超时规则?
【发布时间】:2016-05-15 13:40:44
【问题描述】:

我有一个可以工作的函数,但是如果我增加记录数,就会出现十五秒超时错误。我已经看到“批量化”一个函数,将其分解成块,以欺骗处理器重新开始 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() 函数。我曾希望这会重置超时错误,但结果却是一团糟。

谁能指出我正确的方向?

【问题讨论】:

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

标签: actionscript-3 timeoutexception


【解决方案1】:

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

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

【讨论】:

  • 谢谢你,维斯帕!我的声誉不会显示我的赞成票,但我试过了! :) 不过,我确实接受了你的回答。您为我解决的主要问题是我的代码没有前进到下一帧,从而打破了 15 秒倒计时,对吗?
  • 没错。需要释放代码流(return 来自任何地方),以便 Flash 进入下一帧。
【解决方案2】:

尝试在一两个工人中进行“繁重的工作”。工人是非阻塞的,应该可以解决您的问题。以下是关于它们的更多信息:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Worker.html

【讨论】:

  • 有趣!我以前从未听说过工人。我会做一些阅读。
猜你喜欢
  • 1970-01-01
  • 2018-05-20
  • 2019-10-07
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多