How to maintain and continue FOR-LOOP from the same place

advertisements

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:Object = 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])
}