Let the Flex thread sleep for a while

Categories: Flex; Tagged with: ; @ July 15th, 2011 22:26

Sometimes we use thread.sleep to let the thread stop running for a while in Java, but in Flex, we can’t  control the thread, but sometimes we want to let the method sleeping,  and here is what I’ll do:

public class SleepUtils
{
	// Constructor
	public function SleepUtils() {
	}
	
	/**
	 * Let the thread sleep. 
	 * @param ms Million seconds you want to sleep.
	 * 
	 */	
	public static function startSleep(ms:Number):void {
		var timeBegin:Date = new Date();
		trace("Sleep begin: " + timeBegin.toTimeString());
		while((new Date()).getTime() - timeBegin.getTime() < ms) {
		}
		trace("Sleep end: " + new Date().toTimeString());
	}
	
} // End of class

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.