Macromedia Flex Macromedia Flex
User inactivity timeout example
  Home

Dec 18, 2006 - User inactivity timeout example
uses Timer, getTimer, provides a confirmation period.

<?xml version="1.0" encoding="utf-8"?>
<!-- InactivityTimer.mxml
  This sample app uses a timer to monitor user activity. 
  If inactive for the given interval, it displays a warning and
  begins a confirm interval.  If there is no activity within the set time,
  the application is disabled. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
 initialize="initApp()"
 mouseMove="resetLastActivity()" keyDown="resetLastActivity()">
<mx:Script><![CDATA[
 import mx.controls.Alert;
 
  [Bindable]private var _uintTimeOutInterval:uint = (.1 * 60 * 1000);//minutes * seconds * milliseconds
  [Bindable]private var _uintConfirmInterval:uint = (.1 * 60 * 1000);//minutes * seconds * milliseconds
  private var _timerTimeOut:Timer = new Timer(_uintTimeOutInterval);
  private var _timerConfirm:Timer = new Timer(_uintConfirmInterval) 
  private var _iLastActivity:Number = 0;                                            //The last time of user activity

 /**
  * Called by creation complete.
  * Sets up timer event handlers, starts the timeOut timer.
 */
 private function initApp():void
 {
    _timerTimeOut.addEventListener(TimerEvent.TIMER, onTimeOutTimer);      //set the _timerTimeOut listener
    _timerTimeOut.start();                                                 //start the _timerTimeOut
    _timerConfirm.addEventListener(TimerEvent.TIMER, onConfirmTimer);      //set the _timerTimeOut listener
    ti.setFocus();    //does not work
 }

 /**
  * called by mousmove resets timout
  * clears warning
  */   
 private function resetLastActivity():void
 {
  _iLastActivity = getTimer();    //log time of user activity
  if (_timerConfirm.running)  {    //if confirm timer is running
   _timerConfirm.stop();          //stop it and
   showWorkingStatus("");        //clear the warning
  }
 }//resetLastActivity
 
 /**
   * Run by time event, if timeout interval has elapsed,
   * shows warning, starts confirm timer
  */  
 private function onTimeOutTimer(event:TimerEvent):void
 {
   var iCurTimer:Number = getTimer();                  //get the get timer value
   var iElapsed:Number = iCurTimer - _iLastActivity;   //calculate the elapsed time
   if ( iElapsed >= _uintTimeOutInterval ) {            //if it is greater than the specifice time out
    showWorkingStatus("Move the mouse or type within " + _uintConfirmInterval/1000 + " seconds to stop automatic TIMEOUT.");
    _timerConfirm.start();                             //show the warning, ans start the confirm timer
   }  
 }//onTimeOutTimer 
 
 /**
  * If the interval passes without user activity,
  * do the timeout functionality,
  * in this case, display a message and disable the app.
  */ 
 private function onConfirmTimer(event:TimerEvent):void
 {
  showWorkingStatus("TIMEOUT: Application disabled.");
  _timerConfirm.stop();
  _timerTimeOut.stop();
  this.enabled = false;
  //Do whatever  
 }

 /**
  * example diplay shows timeout and confirm status   
 */   
 public function showWorkingStatus(bText:String):void
 {
  if (bText.length > 0)  {
   hbWorkingStatus.visible = true;
   lblWorkingStatus.text = bText;
  } else  {
   hbWorkingStatus.visible = false;
   lblWorkingStatus.text = "";   
  }
 }//showWorkingStatus 
   
]]></mx:Script> 
<mx:Canvas height="100%" width="100%">
 <mx:Text text="Inactivity Timer example&#13;Warning will appear after {_uintTimeOutInterval/1000} seconds of user inactivity." fontSize="18" />
 <mx:TextInput id="ti" width="200" text="type in here to prevent timeout" x="0" y="50" />
 <mx:VBox id="hbWorkingStatus"  x="50" y="100" width="450" height="25" backgroundColor="#FFFF00"
   backgroundAlpha="50" 
   horizontalAlign="center"  verticalAlign="middle"
   borderStyle="outset" borderThickness="20"
   borderColor="#FF0000" borderSides="#FF0000"
   visible="false"
   mouseDown="showWorkingStatus('')" >
  <mx:Label id="lblWorkingStatus" text="" color="#000000" width="95%" height="100%" />
 </mx:VBox>
</mx:Canvas> 
</mx:Application>

File Details
Created On Dec, 18, 2006 by Tracy Spratt
Last Modified On Dec, 18, 2006 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: General
Type: Tip
Difficulty: Intermediate
Keywords:
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80