0

我正在使用 AS3 提取 XML 数据,其中一个字段是 XML 中的时间字段并显示一小时。我正在获取日期,然后 AS3 根据 XML 中设置的时间加载正确的节点。这一切都很完美 - 我有两次作为变量,一次是系统时间(以 UTC 时间设置),另一次是提前一小时。

这两个变量是 currentHour 和 newHour - 它正在做我想做的一切,但是我现在想在这两个小时之间创建一个倒计时并显示分钟内剩余的时间。

这是完整的代码。 使用 AS3 从 XML 获取时间

这些看起来很简单,但我很难过。我试过这个:

var data:Array = [currentHour, newHour];

var aDate:String = data[0].split(" ")[0];

var dateElements:Array = aDate.split("-");
var date1:Date = new Date();
date1.setMinutes(int(data[0].split(" ")[1].split(":")[0]));
dateElements = data[1].split(" ")[0].split("-");
var date2:Date = new Date();
date2.setMinutes(int(data[1].split(" ")[1]));
var elapse:Number = date2.getTime() - date1.getTime();
trace("minutes: " + date2.getMinutes());

但这是不对的,所以我尝试了这个:

if(currentHour < newHour)
{
var dayDiff:Number = newHour-currentHour;
// make sure it’s in the future
if (dayDiff > 0)
{
var seconds:Number = dayDiff / 1000;
var minutes:Number = seconds / 60;
}
trace(minutes, seconds);
}

如果有人可以帮助我摆脱困境,那就太棒了。谢谢!

4

4 回答 4

1

创建了一个不包含上一个垃圾邮件的新答案,我试图找出实际问题是什么以及程序的期望是什么。总之总结一下:

用户想要,给定一个特定的日期/时间,找出距离下一个完整小时的时间,例如,给定时间 4:47 pm,想要找出距离 5:00 pm 还剩多少分钟和秒。

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.NumberFormatter;
import flash.utils.getTimer;
import flash.utils.Timer;

var timer:Timer = new Timer(250);
var my_date:Date = new Date();
var targetDate:Date = new Date();

//calculate how many total milliseconds left until next whole hour since that is how as3 is using Date-objects
var timeUntilWholeHourMS:Number = (60 - my_date.getMinutes()) * 60 * 1000;
targetDate.setTime(my_date.getTime() + timeUntilWholeHourMS);
//clear the "second and milliseconds" part of the new time since we want the whole hours
targetDate.setSeconds(0, 0);

var secondsLeft:Number = (targetDate.time - new Date().time) / 1000;
//make sure it is in the future
if (secondsLeft > 0) {
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
}

function onTimer(e:flash.events.TimerEvent):void {
    secondsLeft = (targetDate.time - new Date().time)/1000;

    if (secondsLeft > 0) {
        var minutes:Number = Math.floor(secondsLeft/60);
        var seconds:Number = Math.floor(secondsLeft%60);
        trace("minutes left: " + minutes, ", seconds left: " + seconds);
    } else {
        //time limit reached
        timer.removeEventListener(TimerEvent.TIMER, onTimer);
        timer.stop();
        trace("Time limit reached!");
    }
}
于 2013-07-11T22:57:22.703 回答
0

有点不清楚你想如何呈现数据,但这至少是让它工作的方法。您需要将“currentHour & newHour”替换为实际小时数。然后使用适当的代码处理 onTimer 内部的情况。

此代码与其他解决方案的区别在于,它在您开始时需要一个时间戳,然后每当发生计时器事件时,它都会根据该时间戳检查当前时间。这意味着闪光灯定时器是否关闭几毫秒等都没关系。

import flash.events.TimerEvent;
import flash.globalization.NumberFormatter;
import flash.utils.getTimer;
import flash.utils.Timer;

var timer:Timer = new Timer(250);

var currentHour:Number = 14;
var newHour:Number = 15;
var totalSeconds:Number = 0;
var timestampStart:Number = 0;
if(currentHour < newHour) {
    totalSeconds = (newHour - currentHour) * 3600;
    if (totalSeconds > 0) {
        timestampStart = getTimer();
        timer.addEventListener(TimerEvent.TIMER, onTimer);
        timer.start();
    }
}

function onTimer(e:flash.events.TimerEvent):void {
    var secondsRunning:Number = (getTimer() - timestampStart) / 1000;
    var secondsLeft:Number = totalSeconds - secondsRunning;
    if (secondsLeft > 0) {
        var minutes:Number = Math.floor(secondsLeft/60);
        var seconds:Number = Math.floor(secondsLeft%60);
        trace("minutes left: " + minutes, ", seconds left: " + seconds);
    } else {
        //time limit reached
        timer.removeEventListener(TimerEvent.TIMER, onTimer);
        timer.stop();
        trace("Time limit reached!");
    }
}
于 2013-07-11T21:17:44.093 回答
0

我建议您使用 Timer。首先声明一个变量 seconds(在 currentHour 和 newHour 之间):

private var seconds:int;

然后给它赋值

seconds = (newHour - currentHour) * 3600;

然后声明一个每秒计时的计时器(第二个参数告诉它计时多少次):

var timer:Timer = new Timer(1000, seconds)
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

然后创建 timerHandler 将进行所有必要的更新:

function func(e:TimerEvent):void {
    --seconds;
    //if you have some textfield that shows minutes left, update it's text here
    //timeTextfieldText.text = int(seconds / 60) + " minutes left";
    trace(int(seconds / 60));
}
于 2013-07-10T07:05:57.070 回答
-1

您需要添加一个 enterFrame 事件处理程序

private var frameCount:int = 0;

private var diff:int = 3600;//the seconds between the two hours, you could set it here

this.addEventListener(Event.ENTER_FRAME, handler);

private function handler(event:Event):void {
     frameCount++;

     if (frameCount >= stage.frameRate) {

          frameCount = 0;

          diff--;

          if (diff < 0) {
              this.removeEventListener(Event.ENTER_FRAME, handler);
          }
     }

     var minutes:int = diff/60;
}
于 2013-07-10T01:39:57.730 回答