AS3 Simple digital clock


/ Published in: ActionScript 3
Save to your folder(s)

This example expects there to be a dynamic textfield on the stage with the instance name of 'myTextFieldOnStage'. Remember to embed the fonts (Numerals and Colon).


Copy this code and paste it in your HTML
  1. import flash.text.TextField;
  2.  
  3. var timeTextfield:TextField = myTextFieldOnStage;
  4. timeTextfield.text = getFormattedTime();
  5.  
  6. var clockTimer:Timer = new Timer(1000, 0);
  7. clockTimer.addEventListener(TimerEvent.TIMER, onClockTimer);
  8. clockTimer.start();
  9.  
  10. function onClockTimer(e:TimerEvent):void {
  11. timeTextfield.text = getFormattedTime();
  12. }
  13.  
  14. function getFormattedTime():String {
  15. var now:Date = new Date();
  16. var hrs:String = String(now.getHours());
  17. if (hrs.length < 2) {
  18. hrs = "0" + hrs;
  19. }
  20. var mins:String = String(now.getMinutes());
  21. if (mins.length < 2) {
  22. mins = "0" + mins;
  23. }
  24. return hrs + ":" + mins;
  25. }

Report this snippet


Comments


You need to login to view comments.