当前位置: 动力学知识库 > 问答 > 编程问答 >

actionscript 3 - Create AS3 countdown clock without using users system Clock

问题描述:

I need to create an as3 countdown clock for a TV display with no system clock.

How can I utilize the Date object for this?

Here is my code so far:

var targetDate:Date = new Date(2015, 6, 5, 19, 00, 00);

var dateStr:Date = new Date(2015, 5, 25, 18, 56, 00);

addEventListener(Event.ENTER_FRAME, loop);

function loop(e:Event):void{

var nowDate:Date = new Date(dateStr);

var ms:Number = targetDate.getTime() - nowDate.getTime();

var sec:Number = Math.floor(ms/1000);

var min:Number = Math.floor(sec/60);

var hr:Number = Math.floor(min/60);

var day:Number = Math.floor(hr/24);

sec = sec % 60;

min = min % 60;

hr = hr % 24;

daytxt.text = day.toString();

hrtxt.text = (hr < 10) ? "0"+hr.toString() : hr.toString();

mintxt.text = (min < 10) ? "0"+min.toString() : min.toString();

sectxt.text = (sec < 10) ? "0"+sec.toString() : sec.toString();

//sec--;

trace(dateStr);

}

I'm trying to pass in the date parameters to the Date() constructor, but I cannot get it to count down.

网友答案:

Instead of using enterframe it's better to use Timer. You should set it for a 1000 millisecond ie one second . Here is a good tutorial for creating a timer.

分享给朋友:
您可能感兴趣的文章:
随机阅读: