我需要一些关于时钟的建议
I need some suggetions on a clock
我有一个工作时钟,但我需要它来倒计时,而不是 运行 前进
这是我目前正在使用的代码
import flash.utils.Timer;
import flash.events.TimerEvent;
var looper: Timer = new Timer(100);
looper.start();
looper.addEventListener(TimerEvent.TIMER, loopF);
function loopF(event:TimerEvent):void{
var time: Date = new Date();
//time variables
var hours:* = time.getHours();
var minutes:* = time.getMinutes();
var seconds:* = time.getSeconds();
var hourStrg:String;
var minuteStrg:String;
var secondStrg:String;
//time text
if(String(seconds).length < 2){
seconds = "0" + seconds;
}
if(String(minutes).length < 2){
minutes = "0" + minutes;
}
if(hours > 11){
ampm_txt.text = "PM";
} else {
ampm_txt.text = "AM";
}
if(hours > 12){
hours = hours - 12;
}
if (String(hours).length < 2){
hours = "0" + hours;
}
time_txt.text = hours + ":" + minutes + ":" + seconds;
}
也许有人知道一种将其更改为倒计时的简单方法
根据您的代码,您可以执行如下操作:
在启动计时器之前创建一个全局 Date 对象,并将计时器的延迟设置为 1000 毫秒。
在定时器的回调函数中,我们可以获得定时器自启动以来被触发的次数。有了这个数字(以及定时器每秒触发一次的知识),我们可以简单地从全局 Date 对象中减去这个值乘以 1000,使其倒数。
import flash.utils.Timer;
import flash.events.TimerEvent;
var looper:Timer = new Timer(1000);
var now:Date=new Date();
looper.start();
looper.addEventListener(TimerEvent.TIMER, loopF);
function loopF(event:TimerEvent):void{
var time:Date = new Date(now.getTime() - Timer(event.currentTarget).currentCount * 1000);
//time variables
var hours:* = time.getHours();
var minutes:* = time.getMinutes();
var seconds:* = time.getSeconds();
var hourStrg:String;
var minuteStrg:String;
var secondStrg:String;
//time text
if(String(seconds).length < 2){
seconds = "0" + seconds;
}
if(String(minutes).length < 2){
minutes = "0" + minutes;
}
if(hours > 11){
ampm_txt.text = "PM";
} else {
ampm_txt.text = "AM";
}
if(hours > 12){
hours = hours - 12;
}
if (String(hours).length < 2){
hours = "0" + hours;
}
time_txt.text = hours + ":" + minutes + ":" + seconds;
}
我有一个工作时钟,但我需要它来倒计时,而不是 运行 前进 这是我目前正在使用的代码
import flash.utils.Timer;
import flash.events.TimerEvent;
var looper: Timer = new Timer(100);
looper.start();
looper.addEventListener(TimerEvent.TIMER, loopF);
function loopF(event:TimerEvent):void{
var time: Date = new Date();
//time variables
var hours:* = time.getHours();
var minutes:* = time.getMinutes();
var seconds:* = time.getSeconds();
var hourStrg:String;
var minuteStrg:String;
var secondStrg:String;
//time text
if(String(seconds).length < 2){
seconds = "0" + seconds;
}
if(String(minutes).length < 2){
minutes = "0" + minutes;
}
if(hours > 11){
ampm_txt.text = "PM";
} else {
ampm_txt.text = "AM";
}
if(hours > 12){
hours = hours - 12;
}
if (String(hours).length < 2){
hours = "0" + hours;
}
time_txt.text = hours + ":" + minutes + ":" + seconds;
}
也许有人知道一种将其更改为倒计时的简单方法
根据您的代码,您可以执行如下操作: 在启动计时器之前创建一个全局 Date 对象,并将计时器的延迟设置为 1000 毫秒。
在定时器的回调函数中,我们可以获得定时器自启动以来被触发的次数。有了这个数字(以及定时器每秒触发一次的知识),我们可以简单地从全局 Date 对象中减去这个值乘以 1000,使其倒数。
import flash.utils.Timer;
import flash.events.TimerEvent;
var looper:Timer = new Timer(1000);
var now:Date=new Date();
looper.start();
looper.addEventListener(TimerEvent.TIMER, loopF);
function loopF(event:TimerEvent):void{
var time:Date = new Date(now.getTime() - Timer(event.currentTarget).currentCount * 1000);
//time variables
var hours:* = time.getHours();
var minutes:* = time.getMinutes();
var seconds:* = time.getSeconds();
var hourStrg:String;
var minuteStrg:String;
var secondStrg:String;
//time text
if(String(seconds).length < 2){
seconds = "0" + seconds;
}
if(String(minutes).length < 2){
minutes = "0" + minutes;
}
if(hours > 11){
ampm_txt.text = "PM";
} else {
ampm_txt.text = "AM";
}
if(hours > 12){
hours = hours - 12;
}
if (String(hours).length < 2){
hours = "0" + hours;
}
time_txt.text = hours + ":" + minutes + ":" + seconds;
}