If you want to upload a big file to server or do a long time action in Flex/ActionScript like HTTPService to communicate with an external server that hosts the database, a time delay function like Sleep() will be needed.

So, is there a sleep function in flex 2 ? Run multi-thread, one sleep and wait the result of this thread and the other will be continue.

Unfortunatly, because the flash player, from our perspective, is single threaded so we can not do that.

Sleep-and-wait-in-Flex

But we can flash.utils.Timer to get the same effect:

  1. setTimout( yourFunction ,200);

This function will call yourFunction after waiting 200 milliseconds.

The explanation of setTimeout() function:

Runs a specified function after a specified delay (in milliseconds).

Instead of using this method, consider creating a Timer object, with the specified interval, using 1 as the repeatCount parameter (which sets the timer to run only once).

If you intend to use the clearTimeout() method to cancel the setTimeout() call, be sure to assign the setTimeout() call to a variable (which the clearTimeout() function will later reference). If you do not call the clearTimeout() function to cancel the setTimeout() call, the object containing the set timeout closure function will not be garbage collected.

setTimeout():

Runs a specified function after a specified delay (in milliseconds).

Instead of using this method, consider creating a Timer object, with the specified interval, using 1 as the repeatCount parameter (which sets the timer to run only once).

If you intend to use the clearTimeout() method to cancel the setTimeout() call, be sure to assign the setTimeout() call to a variable (which the clearTimeout() function will later reference). If you do not call the clearTimeout() function to cancel the setTimeout() call, the object containing the set timeout closure function will not be garbage collected.

Here is the list of functions for timing code execution: http://livedocs.adobe.com/flex/3/langref/flash/utils/package.html

For help, advice, tips and tricks, challenges, feel free to visit ourDoNotYet forum or Submit your good resource to share.

Reminder: Unless stated otherwise, all resources published on this site are NOT for commercial use. To use any resource from this site for commercial purposes, please contact the author.

Related Posts