在as项目里用timer和timertask做一个样式好看显示时间日期和今天是星期几的数字时钟
时间: 2023-05-10 15:55:42 浏览: 103
可以使用以下代码实现:
```
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
var clock:TextField = new TextField();
addChild(clock);
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, updateClock);
timer.start();
function updateClock(event:TimerEvent):void {
var date:Date = new Date();
var hours:String = addZero(date.getHours());
var minutes:String = addZero(date.getMinutes());
var seconds:String = addZero(date.getSeconds());
var day:String = getDayOfWeek(date.getDay());
var dateString:String = hours + ":" + minutes + ":" + seconds + " " + day;
clock.text = dateString;
}
function addZero(num:int):String {
return (num < 10) ? "0" + num.toString() : num.toString();
}
function getDayOfWeek(day:int):String {
var days:Array = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return days[day];
}
```
这段代码使用了 Timer 类来每秒更新一次时间,并使用 TextField 来显示时间和星期几。函数 addZero 和 getDayOfWeek 分别用于格式化时间和获取星期几的字符串。
阅读全文