开发者

How to Hold and Continue FOR-LOOP from the same spot

Hi guys I got a for/foreach loop the calls a function inside of it. The prolem is that the function being-called doesnt fininsh it's job before the loop goes over again/

Here is my code:

            private function ziv(result:Array,fail:Object):void
        {
            var i:uint = result.length;
            for each(var f:Object in result)
            {
                var item:Obje开发者_如何学Goct = f;
                notfriend=item;
                FacebookDesktop.fqlQuery("SELECT uid1, uid2 FROM friend WHERE uid1 = me() AND uid2 = "+item.id,myfriends);

            }
        }

        private function myfriends(result:Object,fail:Object):void
        {
            if (result.length != 0)
            myfriend.addItem(notfriend);
        }

As you can see i want to add an item (notfriend) in MYFRIENDS function, "notfriend" is defined inside the loop, but by the time "MYFRIENDS" function finish loading the item already changes to the next item even though i was originally refering to the previous item. Is there a way to maybe hold the FORloop until "myfriends" function finish loading.

I wanted to use Eventlistener on "myfriends" function , but then what do i do to stop\hold the loop? all i know is BREAK-which destroyes the FOR-LOOP and CONTINUE-which continue the loop from the next iterate.

10x alot , im really braking my head here!


Maybe look at not using a for loop and do your loop manually (example below uses recursion)

ie

private var index=0;
private function processArray() {
  proccessArrayItem(array[index])
}
private function proccessArrayItem(obj) {

   //after complete then call function recursively
   index++;
   if (index<array.length) proccessArrayItem(array[index])
}


U can use recursive function in some cases

like

 private function setZoom():void
        {
            var zoomLevel:Number = new Number(myPanel);
            if(zoomLevel != 100)
            {
                if(zoomLevel < 100)
                {
                    myPanel.zoomIN();
                    setZoom();
                }
                else
                {
                    myPanel.zoomOUT();
                    setZoom();
                }
            }
        }

but some cases we cant do that

by only one solution we can achieve by

using TimerFunctions

setTimeout() or callLater();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜