如何在actionscript 3中的固定时间调用某个事件?

时间:2010-12-05 14:31:40

标签: actionscript-3

我想每天eventX调用14:30,如何在actionscript 3中完成这类工作?

3 个答案:

答案 0 :(得分:1)

Adob​​e AS3 Alarm Clock example应该告诉您需要了解的所有信息。特别是,setAlarm方法会引起人们的兴趣。

答案 1 :(得分:0)

您可以使用日期对象 的修改 有人建议闪光灯可能会冻结一秒钟,导致事件不发生。这是有效的 - 我已经更新了它,所以这不再是一个问题。

var currDate:Date = new Date();

var targetHour:int = 14;
var targetMinute:int = 30;

var startHour:int = currDate.getHours();
var startMinute:int = currDate.getMinutes();

var eventTriggered:Boolean = false;

// you can use an enterframe or a timer event here, doesn't matter which
addEventListener(Event.ENTER_FRAME, onCheckTime);

function onCheckTime(evt:Event):void{
  currDate = new Date();

  var hours:int = currDate.getHours();
  var minutes:int = currDate.getMinutes();

  // in order for this not to work flash would need to freeze for an entire minute
  // not likely 
  if (hours == 14 && minutes == 30){
     trigger();
  }

  // if flash froze for a whole minute
  if (!eventTriggered){
    if (startHour <= targetHour && startMinute < targetMinute){
      if (hours > targetHour && minutes > targetMinute){
        trigger();
      }
    }
  }
}
function trigger():void{
  // do anything you want here
  eventTriggered = true;
  dispatchEvent(new Event("eventX"));
  removeEventListener(Event.ENTER_FRAME, onCheckTime);
}

答案 2 :(得分:0)

由于上述原因,使用Date()作为重复事件的触发点是不可靠的,并且因为Date类与本地计算机的时间相关联,可以由用户更改。虽然不太可能,但用户可以更改本地计算机上的日期,从不允许它达到目标日期。

但是,如果您使用日期(),我可能会像以下一样接近它,这会给您一分钟(或者您设置 timeOutLength 的任何内容)变量to),要调用 trigger()。如果在没有 trigger()调用的情况下以某种方式传递触发器窗口,则 else 块会调用 trigger()。其余的函数创建一个等待期,直到触发器窗口到期,然后将日期向前滚动一天并重新启动该过程。

var timeOutLength:Number = 1;
var minutesToWaitUntilReset:Number = timeOutLength + 1;
var hasBeenTriggered:Boolean = false;

var targetHour:Number = 2;
var targetMinute:Number = 38;

var currentYear:Number = new Date().getFullYear()
var currentMonth:Number = new Date().getMonth();
var currentDay:Number = new Date().getDate();

addEventListener(Event.ENTER_FRAME, onCheckTime);

function onCheckTime(evt:Event):void
{
    var currentDate = new Date().getTime();
    var targetDate = new Date(currentYear, currentMonth, currentDay, targetHour, targetMinute).getTime();
    var timeOutDate = new Date(currentYear, currentMonth, currentDay, targetHour, (targetMinute + timeOutLength)).getTime();

    if (currentDate >= targetDate && currentDate < timeOutDate && hasBeenTriggered == false)
    {
        tiggerDailyEvent();
    }
    else if(currentDate >= timeOutDate && hasBeenTriggered == false)
    {
        tiggerDailyEvent();
    }
}

function tiggerDailyEvent():void
{
    hasBeenTriggered = true;
    removeEventListener(Event.ENTER_FRAME, onCheckTime);
    addEventListener(Event.ENTER_FRAME, waitForReset);

    // this is where you call your daily event
}

function waitForReset(evt:Event):void
{
    var currentYear = new Date().getFullYear();
    var currentMonth = new Date().getMonth();
    var currentDay = new Date().getDate();

    var currentDate = new Date().getTime();
    var resetDate = new Date(currentYear, currentMonth, currentDay, targetHour, (targetMinute + timeOutLength + minutesToWaitUntilReset)).getTime();

    if(currentDate >= resetDate)
    {
        removeEventListener(Event.ENTER_FRAME, waitForReset);
        reset();
    }
}

function reset():void
{
    hasBeenTriggered = false;

    currentYear = new Date().getFullYear();
    currentMonth = new Date().getMonth();
    currentDay = new Date().getDate() + 1;

    addEventListener(Event.ENTER_FRAME, onCheckTime);
}