I needed a periodic showStatus funcation and was reading the setInterval docs where it suggest to use the Timer class instead.
I gave it a try, it won't work. However setInterval does work
Compiled using flex sdk ''Version 3.3.0 build 4852''
The player's about sends me to http://www.adobe.com/software/flash/about/ ,?which reports ''You have version 10,0,32,18 installed''
Below is the code with setInterval enabeld (works) simply comment this line and uncomment the section that uses a Timer above it to test the other case.
package
{
?import flash.display.Sprite;
?import flash.text.TextField;
?import flash.utils.setInterval;
?import flash.utils.Timer;
?import flash.events.TimerEvent;
?import flash.events.Event;
?public class Main extends Sprite
?{
?private var statusLine:TextField;
?private var statusText:String = '''';
?private var statusTimer:Timer;
?private var timerStep:Number = 100;
?private var elapsedTime:Number = 0;
?public function Main()
?{
?statusLine = new TextField();
?statusLine.width = 400;
?statusLine.height = 400;
?statusLine.multiline = true;
?statusLine.wordWrap = true;
?statusLine.text = ''Starting'';
?addChild(statusLine);
?// NOTE: Adobe livedocs for setInterval says:
?// ''Instead of using the setInterval() method, consider creating
?//?a Timer object, with the specified interval, ...''
?// statusTimer = new Timer(timerStep,0);
?// statusTimer.addEventListener(''timer'',showStatus); // One sample shows it this way
?// statusTimer.addEventListener(TimerEvent.TIMER,showStatus); // The other sample reads like this
?// statusTimer.start();
?// except it does not work! stick with setInterval() for now
?setInterval(showStatus,timerStep);
?}
?private function showStatus():void
?{
?var txt:String = '''';
?elapsedTime += timerStep;
?txt += '' %26gt; '' + Math.round(elapsedTime/100)/10 + '' %26lt; '';
?statusLine.text = txt;
?}
?}
}
Hi,
?Once Check the Following Code. its Working
package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
public class TestTimer extends Sprite
{
private var statusLine:TextField;
?private var statusText:String = '''';
?private var statusTimer:Timer;
?private var timerStep:Number = 100;
?private var elapsedTime:Number = 0;
?private var counter:int=0;
public function TestTimer()
{
statusLine = new TextField();
?statusLine.width = 400;
?statusLine.height = 400;
?statusLine.multiline = true;
?statusLine.wordWrap = true;
?statusLine.text = ''Starting'';
?addChild(statusLine);
?statusTimer=new Timer(100);
?statusTimer.addEventListener(TimerEvent.TIMER,fireTimer);
?statusTimer.start();
}
private function fireTimer(event:TimerEvent):void
{
statusLine.text=''fired Timer''+counter;
counter++;
}
}
}
Timer class does not workYour'e right! the key is to pass the timer event to the call back function.
I made this one change to my original code and it started to work:
I replaced
private function showStatus():void
with
private function showStatus(event:TimerEvent):void
You should have gotten a runtime error if you didn't do that.
Alex Harui
Flex SDK Developer
Adobe Systems Inc.
Blog: http://blogs.adobe.com/aharui
No comments:
Post a Comment