セキュリティなど要求があるアプリケーション(金融や銀行などいろいろね)には、ある規定時間内、ユーザさんが何の操作もしなければ、自動的でログアウトさせてセッションを終了するようになります。setTimeout()関数を利用して、マウスとキーボードの操作をリッスン(MouseEvent.MOUSE_MOVE、MouseEvent.CLICKとKeyboardEvent.KEY_DOWN)して、ユーザのディレイ時間を計算して、規定時間と比べて、超えるとログアウト処理を行います。


流れは簡単ですね。まずsetTimeout()関数の詳細をご覧でください。

setTimeout()関数

  1. public function setTimeout(closure:Function, delay:Number, ... arguments):uint

ミリ秒単位で指定した遅延時間の経過後に、指定した関数を実行します。

このメソッドを使用する代わりに、repeatCount パラメータを 1 (タイマーを 1 回のみ実行する設定) にして、指定した間隔で Timer オブジェクトを作成することを検討してください。 clearTimeout() メソッドを使用して setTimeout() の呼び出しをキャンセルする場合は、必ず次のように、後で setTimeout() メソッドで参照される変数に clearTimeout() の呼び出しを割り当ててください。

  1. var my_timedProcess:uint = setTimeout(my_delayedFunction, 1000);
  2.  
  3. // ...
  4.  
  5. clearTimeout(my_timedProcess);

パラメータ

closure:Function — 実行する関数の名前です。引用符や括弧を使用しないでください。また、呼び出す関数にパラメータを指定しないでください。たとえば、functionName を使用して、functionName()functionName(param) は使用しないでください。

delay:Number — 関数が実行されるまでの遅延時間 (ミリ秒単位) です。

... arguments — closure 関数に渡す引数のオプションのリストです。

戻り値

uint — 時間のプロセスに対する一意の数値識別子です。

以下は全部のサンプルソースコードです。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
  3.     backgroundColor="#FFFFFF" applicationComplete="initApp()" >
  4.    
  5.     <mx:Script>
  6.         <![CDATA[
  7.            
  8.             // Property to hold reference to timeout ID.
  9.             private var activityTimeoutId:uint;
  10.            
  11.             public function initApp() :void
  12.             {
  13.                 // Register the handleUserActivity method as a listener
  14.                 // for all events that indicate user activity.
  15.                 if (stage != null)
  16.                 {
  17.                     stage.addEventListener(MouseEvent.MOUSE_MOVE,
  18.                         handleUserActivity);
  19.                     stage.addEventListener(MouseEvent.CLICK, handleUserActivity);
  20.                     stage.addEventListener(KeyboardEvent.KEY_DOWN,
  21.                         handleUserActivity);
  22.                 }
  23.             }
  24.            
  25.             public function doLogIn() :void
  26.             {
  27.                 // Log in logic goes here. We're keeping it simple for this demo.
  28.                 loginPrompt.visible = false;
  29.                 message.text = "You are logged in.";
  30.                
  31.                 // Set up our auto log out functionality.
  32.                 resetAutoLogOut();
  33.             }
  34.            
  35.             public function doLogOut() :void
  36.             {
  37.                 message.text = "You are logged out.";
  38.                 loginPrompt.visible = true;
  39.             }
  40.            
  41.             public function handleUserActivity(event:Event) :void
  42.             {
  43.                 // Reset the timer whenever use activity is detected.
  44.                 resetAutoLogOut();
  45.             }
  46.            
  47.             public function resetAutoLogOut() :void
  48.             {
  49.                 // Clear old timeout.
  50.                 clearTimeout(activityTimeoutId);
  51.                
  52.                 // Set up a timeout to trigger the log out function
  53.                 // after 30 seconds.
  54.                 var delay:Number = 30 * 1000;
  55.                 activityTimeoutId = setTimeout(doLogOut, delay);
  56.             }
  57.         ]]>
  58.     </mx:Script>
  59.    
  60.     <mx:Label id="message" horizontalCenter="0" text="You are logged out." />
  61.    
  62.     <mx:Panel id="loginPrompt" title="Log In" horizontalCenter="0"
  63.         top="20" horizontalAlign="right">
  64.         <mx:Form>
  65.             <mx:FormItem label="Username:">
  66.                 <mx:TextInput id="userNameInput" />
  67.             </mx:FormItem>
  68.             <mx:FormItem label="Password">
  69.                 <mx:TextInput id="passwordInput" displayAsPassword="true" />
  70.             </mx:FormItem>
  71.         </mx:Form>
  72.         <mx:ControlBar horizontalAlign="center">
  73.             <mx:Button id="submitButton" label="Submit" click="doLogIn()" />
  74.         </mx:ControlBar>
  75.     </mx:Panel>
  76.    
  77. </mx:Application>

参考リソース:
http://livedocs.adobe.com/flex/2_jp/langref/flash/utils/package.html
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postid=322&loc=en_US&productid=2

メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Wednesday, 21st January 2009 by admin

Tags: , , , ,
Posted in Flex | Comments (0) | 3,200 views

Leave a Reply