views:

133

answers:

1

Guys-

I have what I THINK is a simple problem here. I'm calling a basic timer on login success, followed by setting up a couple handlers to refresh some data on a schedule. (I haven't pasted the timer handlers cause i don't think it matters what they do) Anyway, I'm trying to shut OFF those timed refreshes when the app is IDLE, but this:

    if (e.currentTarget.mx_internal::idleCounter > 15000) {

Never gets triggered. I presume it's because of the different way Flex 4 is handling these objects, but I can't find it anywhere in the documentation. Searching for idleCounter comes up empty, even.

protected function getUserByIDResult_resultHandler(event:ResultEvent):void
{

    var sysMan:ISystemManager = FlexGlobals.topLevelApplication.systemManager;
    sysMan.addEventListener(FlexEvent.IDLE, userIdle);

    session.user = event.result as User;

    timer = new Timer(5000);
    timer.addEventListener(TimerEvent.TIMER, timer_short);
    timer.start();

    timer2 = new Timer(10000);
    timer2.addEventListener(TimerEvent.TIMER, timer_long);
    timer2.start();

    currentState='Main';
}


private function userIdle(e:FlexEvent):void {
    if (e.currentTarget.mx_internal::idleCounter > 15000) {
        timer.stop();
        timer2.stop();
    }
    if (e.currentTarget.mx_internal::idleCounter < 15000) {
        if ( timer.running == false) {
            timer.start();
            timer2.start();
        }
    }
}
A: 

So after some more research, I decided that the Idle event was really the way forward. Here's a simplified example (note the timing is "approximate", which is OK for my purposes, and probably most idle timers).

private function userIdle(e:FlexEvent):void {
            idleCounter++;
            trace(idleCounter);
            if (idleCounter > 100) {
                seconds_since_idle += 10;
                idleCounter = 0;
                if (seconds_since_idle > 60) {
                    trace("idle longer than 1 minute");
                    if (timer.hasEventListener(TimerEvent.TIMER)) {
                        timer.removeEventListener(TimerEvent.TIMER, timer_short);
                    }
                }
                if (seconds_since_idle > 180) {
                    trace("idle longer than 3 minutes");
                    sysMan.removeEventListener(FlexEvent.IDLE, userIdle);
                    session.user = null;
                    currentState='Login';
                    Alert.show("You have been logged out due to inactivity.",
                        "Alert", 
                        Alert.OK);
                }
            } 
        }
MBHNYC