0

I have a notification in my app which is triggering too frequently. I want to store a date/time variable when the notification is triggered. And then the next time it will compare the current date/time and only play the notification if it was more than 10 minutes since the last one.

I have found many tutorials online about comparing dates to find out which of the two dates is newer but not this?

4

1 回答 1

2

只需计算日期值之间的差异time并将其与 10 分钟(以毫秒为单位)进行比较:

private const TEN_MINUTES: Number = 1000 * 60 * 10;

// time value of the last notification (milliseconds)
private var lastNotificationTime: Number = NaN;

private function isNewer(currentDate: Date): Boolean
{
    if (currentDate == null)
        return false;
    if (isNaN(lastNotificationTime))
        return true;
    return currentDate.time - lastNotificationTime > TEN_MINUTES;
}

private function notification(): void
{
    var date:Date = new Date();
    if (isNewer(date)) {
        lastNotificationTime = date.time;
        // play notification
    }
}
于 2013-09-03T10:40:54.530 回答