<?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; setInterval()</title>
	<atom:link href="http://blog.flexexamples.com/tag/setinterval/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 setInterval() method</title>
		<link>http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/</link>
		<comments>http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 07:20:33 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[clearInterval()]]></category>
		<category><![CDATA[setInterval()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/</guid>
		<description><![CDATA[<p>The following example shows how you can execute methods at regular intervals using the setInterval() method. It also shows how you can stop the methods from being executed by calling the clearInterval() method and passing the interval&#8217;s unique id returned from the setInterval() method.</p> <p>Full code after the jump.</p> <p></p> <p class="note">It is important to [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can execute methods at regular intervals using the <code>setInterval()</code> method. It also shows how you can stop the methods from being executed by calling the <code>clearInterval()</code> method and passing the interval&#8217;s unique id returned from the <code>setInterval()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-520"></span></p>
<p class="note">It is important to note that the <code>setInterval()</code> method will cause the method to be called continuously until the <code>clearInterval()</code> method is called. If you only want the method to be called once after a delay, you can use the <code>setTimeout()</code> method, or Timer class.</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/setInterval_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/14/creating-timers-using-the-setinterval-method/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

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

            private var interval:uint;

            private function startInt():void {
                interval = setInterval(getTime, 1000); // 1 second

                startButton.enabled = false;
                stopButton.enabled = true;
            }

            private function stopInt():void {
                if (interval) {
                    clearInterval(interval);
                    Alert.show("Interval stopped.");

                    startButton.enabled = true;
                    stopButton.enabled = false;
                }
            }

            private function getTime():void {
                arrColl.addItemAt({date:new Date(), timer:getTimer()}, 0);
            }

            private function date_labelFunc(item:Object, col:DataGridColumn):String {
                var dat:Date = item.date as Date;
                return dat.toTimeString();
            }

            private function timer_labelFunc(item:Object, col:DataGridColumn):String {
                return numberFormatter.format(item.timer);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:NumberFormatter id="numberFormatter"
            useThousandsSeparator="true" /&gt;

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

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button id="startButton"
                label="start interval"
                click="startInt();" /&gt;
        &lt;mx:Button id="stopButton"
                label="stop interval"
                enabled="false"
                click="stopInt();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="list"
            dataProvider="{arrColl}"
            verticalScrollPolicy="on"
            width="300"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="date"
                    headerText="date:"
                    labelFunction="date_labelFunc"
                    width="200" /&gt;
            &lt;mx:DataGridColumn dataField="timer"
                    headerText="timer (ms):"
                    labelFunction="timer_labelFunc"
                    textAlign="right"
                    width="100" /&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/setInterval_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/setInterval_test/bin/main.html" width="100%" height="300"></iframe></p>
<p>Of course, that is a relatively simple example. If you want to get slightly more complex, you can pass additional parameters to the <code>setInterval()</code> method which will get passed along to your specified method. In the following example, we pass two parameters to our <code>getTime()</code> method. The first parameter is a simple String object (&#8220;peter&#8221;) and the second parameter is an int returned from the <code>getTimer()</code> method. It is important to note that the <code>getTimer()</code> being passed to the custom <code>getTime()</code> method via <code>setInterval()</code> is only called once, not on every iteration of the interval.</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/setInterval_test_2/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/14/creating-timers-using-the-setinterval-method/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

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

            private var interval:uint;

            private function startInt():void {
                interval = setInterval(getTime, 1000, "peter", getTimer());

                startButton.enabled = false;
                stopButton.enabled = true;
            }

            private function stopInt():void {
                if (interval) {
                    clearInterval(interval);
                    Alert.show("Interval stopped.");

                    startButton.enabled = true;
                    stopButton.enabled = false;
                }
            }

            private function getTime(param1:*, param2:*):void {
                arrColl.addItemAt({date:new Date(), p1:param1, p2:param2, timer:getTimer()}, 0);
            }

            private function date_labelFunc(item:Object, col:DataGridColumn):String {
                var dat:Date = item.date as Date;
                return dat.toTimeString();
            }

            private function timer_labelFunc(item:Object, col:DataGridColumn):String {
                return numberFormatter.format(item[col.dataField]);
            }

            private function diff_labelFunc(item:Object, col:DataGridColumn):String {
                return ((item.timer - item.p2) / 1000).toFixed(2);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:NumberFormatter id="numberFormatter"
            useThousandsSeparator="true" /&gt;

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

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button id="startButton"
                label="start interval"
                click="startInt();" /&gt;
        &lt;mx:Button id="stopButton"
                label="stop interval"
                enabled="false"
                click="stopInt();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="list"
            dataProvider="{arrColl}"
            verticalScrollPolicy="on"
            width="100%"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="date"
                    headerText="date:"
                    labelFunction="date_labelFunc"
                    width="200" /&gt;
            &lt;mx:DataGridColumn dataField="timer"
                    headerText="timer (ms):"
                    labelFunction="timer_labelFunc"
                    textAlign="right"
                    width="100" /&gt;
            &lt;mx:DataGridColumn dataField="p1"
                    headerText="name:"
                    width="100" /&gt;
            &lt;mx:DataGridColumn dataField="p2"
                    headerText="start (ms):"
                    labelFunction="timer_labelFunc"
                    textAlign="right"
                    width="100" /&gt;
            &lt;mx:DataGridColumn headerText="diff (sec):"
                    labelFunction="diff_labelFunc"
                    textAlign="right"
                    width="100" /&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/setInterval_test_2/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/setInterval_test_2/bin/main.html" width="100%" height="300"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Creating timers using the setInterval() method on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/',contentID: 'post-520',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'clearInterval(),setInterval()',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/14/creating-timers-using-the-setinterval-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

