2007-11-26

Don't forget the power of callLater()

Don't forget that while you are developing in Flex your applications will run inside the Flash Player. One must remember that at its core the Flash Player is but a video player, and every Flex movie has but two frames. The first frame is for initialization work (when you see that progress bar appear) and the second frame is where the application executes. The second frame is the one that gets re-drawn X times per second, and the Player determines what parts to re-draw based on information provided by the Flex framework. This is why controls have an invalidateProperties() method, basically telling Flex that "hey I changed, I need to be re-drawn".

This is all fine and dandy and works, hmmm, 95% of the time, but sometimes you fall into these weird situations where things don't occur as you coded them. Happened to me just last week where I was developing a component to include in an email client that allowed the user to type a person's name or email and it would display a list of suggestions. Well once the user clicked outside of the component, the suggestion list should disappear but it didn't. You see sometimes your code won't execute in the same frame of animation as everything else; I don't really know why and probably only a Flash Player engineer can explain it. But what you can do is use Flex to tell the Flash Player to execute something in the next frame (probably when its less busy I guess?). You do this by using the global callLater() function like so:

// use it to call some local function
callLater( someFuncName );

// or direct inline call of a nameless function
callLater(
function() : {
... some code here ...
}
);

I've mostly used the second method because what I normally have to do is very short, like 1-2 lines of code. So for me this solved my problem, as I was able to close the suggestion list I was displaying. And it might do the same for you some day, so don't forget it :)

20 comments:

Christian Nunciato said...

Dude, I just discovered callLater() today -- saved my life. I had (and in some places still have) a crazy bug in some code I'd written to manipulate BitmapAssets in a TileList, and was getting an obscure error ("2025: The supplied object must be a child of the caller") -- and not always, only sometimes. Turns out callLater helped delay processing until the screen had appropriately updated itself. I only happened to flip to it in a book this afternoon, looking for something else. What a find.

Cheers,
Chris

Noj said...

i just figured out how to make my applications a little bit more processor intensive with callLater();

thanks for the info.

Royce said...

I had been going crazy until discovering this. Thanks very much for this invaluable info!

Oh BTW, your anonymous function code sample generates compiler warnings. Try this:

callLater(
function():void{
... some code here ...
}
);

Migs said...

This function just solved a bug that I've had for a long time in one of my applications.

A textArea who's style would always get erased! I tried everything and the only thing that worked was resetting the styles in a callLater function

Thanx! :)

Anonymous said...

酒店上班

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店打工

酒店PT

酒店正職

酒店賺錢

酒店日領

Anonymous said...

酒店打工 酒店兼職
台北酒店 打工兼差 酒店工作 禮服酒店
酒店兼差 酒店上班 酒店應徵 酒店 酒店經紀

Anonymous said...

酒店打工 酒店兼職
台北酒店 打工兼差 酒店工作 禮服酒店
酒店兼差 酒店上班 酒店應徵 酒店 酒店經紀

Anonymous said...

台北酒店 酒店兼差 酒店兼職 酒店

Anonymous said...

酒店兼差 酒店兼職 酒店 台北酒店

Anonymous said...

酒店兼職 酒店 台北酒店 酒店兼差

Anonymous said...

酒店 台北酒店 酒店兼差 酒店兼職

Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

How To Code Flex said...

Thanks for this dude!

How buy Antidepressants said...

This is good site to spent time on. allergy

Anonymous said...

This is good site to spent time on. allergy

Anonymous said...

You have a very good blog that the main thing a lot of interesting and useful! erectile dysfunction Read a useful article about tramadol tramadol

Anonymous said...

I was able to find good info from your blog articles.


Feel free to surf to my website; http://www.converseshopfr.com/