Bạn ghi đè điều này như thế nào?
Chúng tôi chỉ đơn giản sử dụng lớp Ngày cũng trong một tập lệnh sẽ đếm đúng thời gian.
- Tạo một tài liệu AS3 mới
- thêm 3 textbox tên minText, secText, MilText và một nút gọi là start_btn
- Thêm mã này vào frame đầu tiên:
var stt: int; // chúng tôi sử dụng biến này để theo dõi thời gian bắt đầu dưới dạng dấu thời gian
var myTimer: Timer = new Timer (1); // đây là bộ hẹn giờ của chúng tôi
var starttime:Date; // pretty obvious
var actualtime:Date; // pretty obvious
myTimer.addEventListener(TimerEvent.TIMER, stopWatch); // we start counting with this counter
start_btn.addEventListener(MouseEvent.CLICK, startClock); // add a button listener to start the timer
function startClock(event:MouseEvent):void
{
starttime = new Date(); // we get the moment of start
stt = int(starttime.valueOf().toString()); // convert this into a timestamp
myTimer.start(); // start the timer (actually counter)
}
function stopWatch(event:TimerEvent):void
{
actualtime = new Date(); // we get this particular moment
var att:int = int(actualtime.valueOf().toString()); // we convert it to a timestamp
// here is the magic
var sec:int = (Math.floor((att-stt)/1000)%100)%60; // we compute an absolute difference in seconds
var min:int = (Math.floor((att-stt)/1000)/60)%10; // we compute an absolute difference in minutes
var ms:int = (att-stt)%1000; // we compute an absolute difference in milliseconds
//we share the result on the screen
minText.text = String(min);
secText.text = String(sec);
milText.text = String(ms);
}
Tại sao bạn cần dấu thời gian và không sử dụng các chức năng của lớp Ngày?
Bởi vì nếu bạn muốn đếm sự khác biệt giữa hai sự kiện mà bạn có thể dùng chương trình này:
endEvent.seconds - startEvent.seconds
Đó là rất sai nếu sự kiện bắt đầu của bạn xảy ra trong 57'th thứ hai và sự kiện xảy ra trong cuối Thứ hai thứ 17, bạn sẽ nhận được -40 giây thay vì 20 và cứ tiếp tục như vậy.
'flash.utils.getTimer() 'là hiệu quả hơn, theo một câu trả lời – BadFeelingAboutThis