user1551451
user1551451

Reputation: 21

Action Script 3.0 insert a delay

i'm new to AS and i'm trying to manage with some issue.

i'm having the next script

var t:Date = new Date ();
var day=t.getDate();
var month=t.getMonth()+1;
var today=day+"."+month;
var u:Number = 0;

var b_persons:Array = new Array ();

var loader:URLLoader = new URLLoader(new URLRequest("bday.xml"));
var bday_file:XML = new XML();

loader.addEventListener(Event.COMPLETE, endLoad);


function endLoad(all_data:Event)

{
   bday_file = XML(all_data.target.data);

   for each (var person:XML in bday_file.Person) 
   {
      if (person.BD == today) 
      {
          b_persons.push(person.FIO);

          trace (b_persons.length);
      }
   }
}

function assign_txtBdayPerson()
{
    txtBdayPerson.text=b_persons[u];
    trace(b_persons[u]);
    u=u+1;
    if (u >= b_persons.length)
    {clearInterval(delay_assign_txtBdayPerson);}

}

trace ("Test"+b_persons.length);

if (b_persons.length != 0)
    {
        var delay_assign_txtBdayPerson = setInterval(assign_txtBdayPerson,3000);
    }

my xml file has 2 suitable conditions, and the output window shows me the next sequence

Test 0 than 1 and last 2

so it seems that

trace ("Test"+b_persons.length); 

runs earlier than

function endLoad(all_data:Event) 

is completed

could you please help me?

Upvotes: 0

Views: 693

Answers (2)

WORMSS
WORMSS

Reputation: 1664

If you want/need trace ("Test"+b_persons.length); to be run after XML has loaded and been been parsed, you will need to place it either within endload or inside a function that endload will call at the end of its function.

Events may seem different and evil if you are not used to them, but once you get used to them you will see they are much nicer to work with.

Upvotes: 1

skovalyov
skovalyov

Reputation: 2099

It happens, because that trace ("Test"+b_persons.length); is executed in the same "frame" with the actual loading start. endLoad() callback is executed at least in the next frame, when the XML is already loaded. That is the nature of asynchronous flow in Flash.

Upvotes: 0

Related Questions