Vince Lowe
Vince Lowe

Reputation: 3630

Flex compare date to check if 10 mins old

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?

Upvotes: 0

Views: 356

Answers (1)

splash
splash

Reputation: 13327

Just calculate the difference between the time values of your dates and compare it with 10 minutes (in milliseconds):

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
    }
}

Upvotes: 2

Related Questions