我打电话的功能,并增加一个听众对于当能返回的一些数据。当数据是返回的,我需要电话的另一个功能等。

是有一个简单的方法的'链条',这些功能结合在一起,因此,第一个火灾等待听那起火灾的第二个创建一个监听器,等等,直到最后一个电话一个单独的功能定义的开始。它将我想工作上的相同的路线为批装载脚本。

我设想码工作是这样的:

var dataLoader:DataLoader = new DataLoader(onAllComplete, onError);

dataLoader.add(getData1, {args1, args2}, listnerType.DATA_LOADED);
dataLoader.add(getData2, {args3, args4}, listnerType.DATA_LOADED);
dataLoader.add(getData3, {args5, args6}, listnerType.DATA_LOADED);

dataLoader.start();

private function onAllComplete(e:Array):void {
  //where e contains an array of all the event results
}
private function onError(e:Event):void {
  //will fire if there are any errors along the way
}

谢谢, Josh

有帮助吗?

解决方案

我会做一些简单的事情:(另外,这是排序伪代码,你需要正确的错误事件和东西)

var numLoaded:int = 0;
var numError:int = 0;
var loadingIndex:int = 0;

var itemsToLoad:Array = ['img1.jpg', 'img2.jpg', 'img3.jpg'];

public function startLoading():void{
     loader.load(itemsToLoad[loadingIndex];
     loader.addEventListener(Event.COMPLETE, completeHandler);
}

public function completeHandler(event:Event):void{
     loadingIndex++;
     numLoaded++;
     if(numLoaded + numError >= itemsToLoad.length){
          onAllItemsComplete();
     }else{
          loader.load(itemsToLoad[loadingIndex];
     }
}

public function errorHandler(event:Event):void{
     loadingIndex++;
     numError++;
     if(numLoaded + numError >= itemsToLoad.length){
          onAllItemsComplete();
     }else{
        loader.load(itemsToLoad[loadingIndex]; 
     }
}

其他提示

在那里,这样做。这里的AS3码:

package com.vpg.rns.util {

    public class AsynchIterator {
        private var iteratorPosition:int;
        private var iterableObjects:Array;
        private var onApply:Function;
        private var onComplete:Function;
        private var done:Boolean;

        public function get position() { return iteratorPosition; }

        public function get isDone() { return done; }

        public function get size() { return iterableObjects.length; }

        /** Create an iterator that will call the given function repeatCall once for each object in iterableObjects, before finally calling completeCall once at the end.
         * The calls will be made asynchronously, with event handlers used to stitch it all together.
         *
         * @param iterableObjects ....... Every object in this array will be passed as the first argument to repeatCall, in order.
         * @param repeatCall ............ This function will be called once for each object in iterableObjects. Its signature is repeatCall(Object, Function).
         * @param completeCall .......... Called once after every item in the array has been processed.
         *
         *
         */
        public function AsynchIterator(iterableObjects:Array, repeatCall:Function, completeCall:Function) {
            this.iteratorPosition = 0; 
            this.iterableObjects = iterableObjects;
            this.onApply = repeatCall;
            this.onComplete = completeCall;
            this.done = false;
        }

        public function iterate():void {
            doNext();
        }

        private function doNext() {
            if (isDone) {
                // Do nothing - already called onComplete. 
            }
            else if (position == size) { 
                done = true;
                onComplete();
            }
            else {
                var obj:Object = iterableObjects[iteratorPosition++];
                onApply(obj, doNext);
            }
        }

    }

}

很明显,你会想添加一个错误的处理程序功能,跟踪的,其失败并取得了成功,加选择快失败而做的-你可以等等。

  • 保罗

我可能会误解你的意图,但是从你所描述的内容来看,你可以在调用处理程序时将它们链接起来......就像:你想做这样的事情但是有更复杂的语法吗?

private function onComplete1(event:Event):void
{
    dataLoader.removeEventListener("onComplete", onComplete1);
    dataLoader.addEventListener("onComplete", onComplete2);

    ... // process event

    dataLoader.load(); // I don't remember the exact calls...
}

private function onComplete2(event:Event)void
{
    dataLoader.removeEventListener("onComplete", onComplete1);
    dataLoader.addEventListener("onComplete", onComplete2);

    ... // process event

    dataLoader.load(); // I don't remember the exact calls...
}

该界面对我来说很好看。实现取决于函数如何返回其数据。 AS3不支持线程,因此您必须编写getData()函数以异步运行。如果您正在从远程站点加载数据或者这很容易,只需使用内置的加载器函数,使用getBytesLoaded()来告知它们何时完成,并在每个人加载时调用OnComplete回调函数。

如果你只是做了很长时间的计算,那么你将不得不将其分解。类似的东西:

class Computation {
    function Step() {/* ... */}
    function IsFinished() {/* ... */}
    function GetResult() {/* ... */}
}

为您需要执行的每种计算子类化类似的东西,然后将实例传递到数据加载器中。每帧都有一次Step(),并在完成后调用OnComplete回调。

splinklibrary 包含可以很好地处理链式异步操作的类。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top