I want to invoke eventX
every day at 14:30
,how to do this kind of job in actionscrip开发者_StackOverflowt 3
?
The Adobe AS3 Alarm Clock example should tell you all you need to know. In particular, the setAlarm
method will be of interest.
You can use the date object EDIT it was suggested that flash might freeze for a second causing the event not to fire. This is valid - I've updated it so this is no longer a problem.
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);
}
Using Date() as a trigger point for a repeating event is unreliable for the reasons mentioned and also because the Date class is tied the the local machine's time, which can be changed by the user. While unlikely, the user could change the date on the local machine, never allowing it to reach the target date.
However, if you were to use Date(), I might approach it like the following, which would give you a full minute (or whatever you set the timeOutLength variable to), to have trigger() called. The else block calls trigger() if the trigger window is somehow passed without trigger() getting called. The rest of the functions create a waiting period until the trigger window has expired, and then rolls the date forward one day and restarts the process.
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);
}
精彩评论