Using FlexEvent.IDLE to Determine Inactivity

A FlexEvent.IDLE event will dispatch every 100 milliseconds when there has been no keyboard or mouse activity for 1 second.  So, if you want to have some other method fire off after the user is inactive for 5 minutes (for example), you would set it up as detailed below.

Be sure to import the following classes.  IDLE needs to be bound to a SystemManager instance and we will need to reference the mx_internal namespace later on:

1
2
3
import mx.managers.SystemManager;
import mx.events.FlexEvent;
import mx.core.mx_internal;

You’ll need to tell Flex that you want to use mx_internal as a namespace to access those properties within the SystemManager class. SytemManager has an “idleCounter” property which is super useful to access- but it is scoped to mx_internal and is not normally accessible. Trying to access it without these steps will throw an error:

1
use namespace mx_internal;

SystemManager is automatically instantiated as a part of every Flex app, so we do not need to do this manually. We will, however, need to add an event listener for FlexEvent.IDLE:

1
this.systemManager.addEventListener(FlexEvent.IDLE, userIdle);

Construct the callback method. Five minutes is equal to 300000 milliseconds… divided by 100 ticks and the number we need to check against is 3000. Of course, you’ll probably want something a little shorter in duration for testing:

1
2
3
4
5
private function userIdle(e:FlexEvent):void {
	if(e.currentTarget.mx_internal::idleCounter == 3000){
		//do something!
	}
}

Note that we prefix the idleClounter property with the mx_internal namespace.

That’s it! Now we have a sweet little activity monitor in our app. When activity is detected, idleCounter will automatically reset as well.

15 thoughts on “Using FlexEvent.IDLE to Determine Inactivity

  1. Thanks for your post, currently I will be implementing the same solution to track user’s inactivity in Flex app based on FlexEvent.IDLE event

    And I find mx_internal::idleCounter to be useful too!

  2. Hi,
    This is very useful, but has a problem. It works perfect until you show a
    TittleWindow, if you move the mouse over the window, the counter don’t restarts, I
    mean, the FlexEvent.IDLE event is not fired when you have opened a TitleWindow.

  3. Thanks a lot for this. I was using a similar solution but recently discovered an intermittent flaw with it. Using the internal idleCounter seems to have resolved my issue.

  4. Thanks for the post, it really helps. I am from China, I also have only recently started to do about flex + Java project, have a problem, the unified session expired on error handling, I want to use your this way, but not very familiar with, there’s no better way to solve, thank you, can send my mailbox, in times thanks! Hope to know you!

  5. Hi

    What happens, when a user changes a tab in his web browser? I have noticed that an idle event stops until the tab is changed back to the app. Is there a way to change this behavior?

  6. Actually I was wrong. An idle event of course goes on and detects movement in other tabs, so I had to play around with activate and deactivate events to start/stop an idle event.

  7. Hi Joseph
    I am newbie with Flex. I followed your example on Flex.IDEL and it worked for me. Thanks a lot. But in QA I am having some weird things.
    1. When browser window is minimized, this event doesn’t trigger.
    2. When some flash contents are running in same or different browser, this event sometime fires and sometimes not.

    Can you help me out in figuring out why this is happening and how I can fix it.

    Thanks in Advance.
    Regards,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">