我需要时钟上的一些建议

时间:2019-04-07 02:55:49

标签: actionscript-3

我有一个工作时钟,但我不需要倒计时,而是倒计时 这是我目前正在使用的代码

 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;

}

也许有人知道一种简单的方法来将其改为倒数

1 个答案:

答案 0 :(得分:1)

根据您的代码,您可以执行以下操作: 在启动计时器之前创建一个全局Date对象,并将计时器的延迟设置为1000ms。

在计时器的回调函数中,我们可以获取自计时器启动以来已被触发的次数。有了这个数字(并知道计时器每秒触发一次),我们可以简单地从全局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;

}