Introduce to setTimeOut function and Timer class

แนะนำการจัดการเวลาโดยใช้ setTimeOut function และ Timer


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<s:layout>
<s:VerticalLayout/>
</s:layout>

<fx:Script>

<![CDATA[

import flash.utils.setTimeout;
import mx.controls.Text;

private var timerOnce:Timer;
private var timerRepeat:Timer;

private function onBt1Click():void{

var i1:int = 1;
var i2:int = 2;
trace('BT1 :: i1=' + i1 + ', i2=' + i2 );

setTimeout(  function( i1:int, i2:int ):void{ trace('BT1 passed 1000 ms :: i1=' + i1 + ', i2=' + i2 ); }, 1000, i1, i2 );

i1 = 11;
i2 = 22;
trace('BT1 :: i1=' + i1 + ', i2=' + i2 );
}

private function onBt2Click():void{

var i1:int = 1;
var i2:int = 2;
trace('BT2 :: i1=' + i1 + ', i2=' + i2 );

setTimeout( onSetTimeOut, 1000, i1, i2 );

i1 = 11;
i2 = 22;
trace('BT2 :: i1=' + i1 + ', i2=' + i2 );
}

private function onSetTimeOut(i1:int, i2:int):void{
trace('BT2 passed 1000 ms :: i1=' + i1 + ', i2=' + i2 );
}

private function onTimerOnceClick():void{
if( timerOnce == null ){
timerOnce = new Timer( 1000, 1 ); // 1000 ms round
timerOnce.addEventListener(TimerEvent.TIMER, onTimerOnceRun);
timerOnce.start();
}
}

private function onTimerOnceRun(event:TimerEvent):void{
trace( 'timerOnce.currentCount=' + timerOnce.currentCount );
}

private function onTimerRepeatClick():void{
if( timerRepeat == null ){
timerRepeat = new Timer( 1000 ); // 1000 ms round
timerRepeat.addEventListener( TimerEvent.TIMER, onTimerRepeatRun);
timerRepeat.start();
}
}

private function onTimerRepeatRun(event:TimerEvent):void{
trace( 'timerRepeat.currentCount=' + timerRepeat.currentCount );
}

private function onTimerStartStop():void{
if( timerRepeat != null ){
if( timerRepeat.running ){
timerRepeat.stop();
}else{
timerRepeat.start();
}
}
}

]]>

</fx:Script>

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:Button label="set time out 1" click="onBt1Click()"/>

<s:Button label="set time out 2" click="onBt2Click()"/>

<s:Button label="timer once" click="onTimerOnceClick()"/>

<s:HGroup>

<s:Button label="timer repeat" click="onTimerRepeatClick()"/>

<s:Button label="Start or Stop" click="onTimerStartStop()"/>

</s:HGroup>

</s:Application>

Advertisement

About knopsod
Developer by Flex/Flash, ActionScript, PHP, Java, MySQL,PostgreSQL, Oracle, Ebay, PayPal, Alibaba, iOffer รับปรึกษา สอน และพัฒนาซอฟแวร์ ซื้อ ขาย ประมูลสินค้าออนไลน์

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.