spacer spacer spacer spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer
spacer spacer spacer

On detecting idle states in Flex 4

So I’m building a B2B dashboard in Flex, and thought I’d write up a quick post on a problem I didn’t see many online solutions for – well, there are solutions, but they all reference weaksauce flex 2, so I thought I’d go all new and shiny and post this working flex 4 example.

The problem is simple: How do I detect an Idle user, turn off all timed network requests, and — eventually — log off users silly enough to walk away from my interface?


spacer

Example: situation this code is meant to save my application from.

So Here’s a somewhat disjointed code sample. This isn’t a working application, and is missing a few helper functions, and, uh, any interactivity whatsoever – but should get anyone where they need to go.

Tada:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <s:Application xmlns:fx="ns.adobe.com/mxml/2009"
  4.       xmlns:s="library://ns.adobe.com/flex/spark"
  5.       xmlns:mx="library://ns.adobe.com/flex/mx"
  6.       mouseMove="application_mouseMoveHandler(event)">
  7.  <fx:Script>
  8.   <![CDATA[
  9.  
  10.    public var idleCounter:int;
  11.    public var sysMan:ISystemManager;
  12.    public var secondsSinceIdle:int = 0;
  13.  
  14.    //create your timer objects to do, you know, stuff. Below, a function called timer_short
  15.  
  16.    //this is my function for handling grabbing a user after a successful login
  17.    protected function getUserByIDResult_resultHandler(event:ResultEvent):void
  18.    {
  19.     sysMan = FlexGlobals.topLevelApplication.systemManager;
  20.     sysMan.addEventListener(FlexEvent.IDLE, userIdle);
  21.     session.user = event.result as User;
  22.     timer.addEventListener(TimerEvent.TIMER, timer_short);
  23.     timer.start();
  24.     currentState='Main';
  25.    }
  26.  
  27.    private function userIdle(e:FlexEvent):void {
  28.     idleCounter++;
  29.     trace(idleCounter);
  30.     if (idleCounter > 100) {
  31.      secondsSinceIdle += 10;
  32.      idleCounter = 0;
  33.      if (secondsSinceIdle > 60) {
  34.       trace("idle longer than 1 minute");
  35.       if (timer.hasEventListener(TimerEvent.TIMER)) {
  36.        timer.removeEventListener(TimerEvent.TIMER, timer_short);
  37.       }
  38.      }
  39.      if (secondsSinceIdle > 180) {
  40.       trace("idle longer than 3 minutes");
  41.       sysMan.removeEventListener(FlexEvent.IDLE, userIdle);
  42.       session.user = null;
  43.       currentState='Login';
  44.       Alert.show("You have been logged out due to laziness and despondency.",
  45.        "Alert",
  46.        Alert.OK);
  47.      }
  48.     }
  49.    }
  50.  
  51.    protected function application_mouseMoveHandler(event:MouseEvent):void
  52.    {
  53.     secondsSinceIdle = 0;
  54.     idleCounter = 0;
  55.     if (!timer.hasEventListener(TimerEvent.TIMER) && session.user != null) {
  56.      timer.addEventListener(TimerEvent.TIMER, timer_short);
  57.     }
  58.    }
  59.   ]]>
  60.  </fx:Script>
  61. </s:Application>

Also, thanks to Razorlight for some WordPress assistance getting this page to appear correctly.

Tags: blog, flex, idle, programming

This entry was posted on Monday, July 5th, 2010 at 10:37 am and is filed under flex. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • Gromitski

    What if somone will change his browser’s tab? I have noticed that an idle event doesn’t work in a situation like this.

  • Anonymous

    Good question – I will investigate and confirm!

  • Gromitski

    Actually bad question – I was wrong. The idle event goes on and detects movement in other tabs. I had to play around with activate and deactivate events.

  • Anonymous

    Great, good news. That’s what I expected.

  • www.facebook.com/profile.php?id=756812042 Paul Mansfield Keefe

    Why not just do this:

    public function userIdle(event:FlexEvent):void{ 
    // userIdle fires every 100ms 

    if (event.currentTarget.mx_internal::idleCounter == 600){   
    // after 60 seconds of idle time we do something   
    // the idleCounter only resets after the user has  
    // been active again…so this area only runs  
    // once for each idle time that extends for a minute and resets itself
     }

    }

  • Nicolas

    Hello,

    Just a little question :
    Why a Timer ???

    That work with any timer !!

  • Anonymous

    Not sure what you mean? And this post is probably a little out of date at this point spacer

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.