<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; delay</title>
	<atom:link href="http://blog.flexexamples.com/tag/delay/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating timers using the Timer class</title>
		<link>http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/</link>
		<comments>http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 16:36:58 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[currentCount]]></category>
		<category><![CDATA[delay]]></category>
		<category><![CDATA[repeatCount]]></category>
		<category><![CDATA[running]]></category>
		<category><![CDATA[timerComplete]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/</guid>
		<description><![CDATA[<p>In previous examples, we saw how to use the <a href="http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/">setInterval()</a> and <a href="http://blog.flexexamples.com/2008/02/15/creating-timers-using-the-settimeout-method/">setTimeout()</a> methods to create simple timers.</p> <p>The following example shows how you can execute code at regular intervals using the Timer class. It also shows you how you can create timers that repeat continuously as well as timers which only execute a [...]]]></description>
			<content:encoded><![CDATA[<p>In previous examples, we saw how to use the <a href="http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/">setInterval()</a> and <a href="http://blog.flexexamples.com/2008/02/15/creating-timers-using-the-settimeout-method/">setTimeout()</a> methods to create simple timers.</p>
<p>The following example shows how you can execute code at regular intervals using the Timer class. It also shows you how you can create timers that repeat continuously as well as timers which only execute a specific number of times.</p>
<p>Full code after the jump.</p>
<p><span id="more-524"></span></p>
<p>The following example shows a very simple timer which runs continuously every 1000 ms (1 second):</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Timer_timer_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private var timer:Timer;

            private function init():void {
                timer = new Timer(1000);
                timer.addEventListener(TimerEvent.TIMER, timer_timer);
                timer.start();
            }

            private function timer_timer(evt:TimerEvent):void {
                var tmr:Timer = evt.currentTarget as Timer;
                var obj:Object = new Object();
                obj.currentCount = tmr.currentCount;
                obj.delay = tmr.delay;
                obj.repeatCount = tmr.repeatCount;
                obj.running = tmr.running;
                arrColl.addItemAt(obj, 0);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ArrayCollection id="arrColl" /&gt;

    &lt;mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="100%"
            rowCount="10"
            verticalGridLines="false"
            verticalScrollPolicy="on"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="currentCount" /&gt;
            &lt;mx:DataGridColumn dataField="repeatCount" /&gt;
            &lt;mx:DataGridColumn dataField="delay" /&gt;
            &lt;mx:DataGridColumn dataField="running" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Timer_timer_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Timer_timer_test/bin/main.html" width="100%" height="300"></iframe></p>
<p>The following example shows a timer which runs exactly 5 times, and then dispatches a <code>timerComplete</code> event:</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Timer_repeatCount_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.Alert;

            private var timer:Timer;

            private function init():void {
                timer = new Timer(1000, 5);
                timer.addEventListener(TimerEvent.TIMER, timer_timer);
                timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_timerComplete);
                timer.start();
            }

            private function timer_timer(evt:TimerEvent):void {
                var tmr:Timer = evt.currentTarget as Timer;
                var obj:Object = new Object();
                obj.currentCount = tmr.currentCount;
                obj.delay = tmr.delay;
                obj.repeatCount = tmr.repeatCount;
                obj.running = tmr.running;
                arrColl.addItemAt(obj, 0);
            }

            private function timer_timerComplete(evt:TimerEvent):void {
                var tmr:Timer = evt.currentTarget as Timer;
                Alert.show(tmr.currentCount + " of " + tmr.repeatCount, "Timer complete");

                // Call the Timer instance's timer event handler.
                timer_timer(evt);
            }

            private function resetTimer():void {
                arrColl = new ArrayCollection([]);
                timer.reset();
                timer.start();
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ArrayCollection id="arrColl" /&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button label="Reset" click="resetTimer();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="100%"
            rowCount="10"
            verticalGridLines="false"
            verticalScrollPolicy="on"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="currentCount" /&gt;
            &lt;mx:DataGridColumn dataField="repeatCount" /&gt;
            &lt;mx:DataGridColumn dataField="delay" /&gt;
            &lt;mx:DataGridColumn dataField="running" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Timer_repeatCount_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Timer_repeatCount_test/bin/main.html" width="100%" height="350"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Creating timers using the Timer class on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/',contentID: 'post-524',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'currentCount,delay,repeatCount,running,timerComplete',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/02/20/creating-timers-using-the-timer-class/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

