2010-12-05 56 views

回答

1

Adob​​e AS3 Alarm Clock example應該告訴你所有你需要知道的。特別是,setAlarm方法將是有趣的。

+0

如何將它編譯成一個swc文件,以便我可以直接在我的flex項目中使用它? – fms 2010-12-05 16:04:13

+0

您只需在Flex項目中使用代碼即可。你爲什麼要打包成SWC? – spender 2010-12-05 23:45:25

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); 
} 
0

使用日期()作爲觸發點的重複事件的原因提及並且是不可靠的,因爲也Date類被綁定的本地機器的時間,這可以由用戶來改變。雖然不太可能,但用戶可以更改本地計算機上的日期,絕不允許它達到目標日期。

但是,如果你使用日期(),我可能接近它像下面這樣,這將讓你一分鐘(或任何你設置timeOutLength變量),有觸發( )叫。 其他塊調用觸發器()如果觸發器窗口以某種方式傳遞,但不觸發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); 
} 
相關問題