セキュリティなど要求があるアプリケーション(金融や銀行などいろいろね)には、ある規定時間内、ユーザさんが何の操作もしなければ、自動的でログアウトさせてセッションを終了するようになります。setTimeout()関数を利用して、マウスとキーボードの操作をリッスン(MouseEvent.MOUSE_MOVE、MouseEvent.CLICKとKeyboardEvent.KEY_DOWN)して、ユーザのディレイ時間を計算して、規定時間と比べて、超えるとログアウト処理を行います。
流れは簡単ですね。まずsetTimeout()関数の詳細をご覧でください。
setTimeout()関数
- public function setTimeout(closure:Function, delay:Number, ... arguments):uint
ミリ秒単位で指定した遅延時間の経過後に、指定した関数を実行します。
このメソッドを使用する代わりに、repeatCount パラメータを 1 (タイマーを 1 回のみ実行する設定) にして、指定した間隔で Timer オブジェクトを作成することを検討してください。 clearTimeout() メソッドを使用して setTimeout() の呼び出しをキャンセルする場合は、必ず次のように、後で setTimeout() メソッドで参照される変数に clearTimeout() の呼び出しを割り当ててください。
- var my_timedProcess:uint = setTimeout(my_delayedFunction, 1000);
- // ...
- clearTimeout(my_timedProcess);
パラメータ
closure:Function — 実行する関数の名前です。引用符や括弧を使用しないでください。また、呼び出す関数にパラメータを指定しないでください。たとえば、functionName を使用して、functionName() や functionName(param) は使用しないでください。
delay:Number — 関数が実行されるまでの遅延時間 (ミリ秒単位) です。
... arguments — closure 関数に渡す引数のオプションのリストです。
戻り値
uint — 時間のプロセスに対する一意の数値識別子です。
以下は全部のサンプルソースコードです。
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
- backgroundColor="#FFFFFF" applicationComplete="initApp()" >
- <mx:Script>
- <![CDATA[
- // Property to hold reference to timeout ID.
- private var activityTimeoutId:uint;
- public function initApp() :void
- {
- // Register the handleUserActivity method as a listener
- // for all events that indicate user activity.
- if (stage != null)
- {
- stage.addEventListener(MouseEvent.MOUSE_MOVE,
- handleUserActivity);
- stage.addEventListener(MouseEvent.CLICK, handleUserActivity);
- stage.addEventListener(KeyboardEvent.KEY_DOWN,
- handleUserActivity);
- }
- }
- public function doLogIn() :void
- {
- // Log in logic goes here. We're keeping it simple for this demo.
- loginPrompt.visible = false;
- message.text = "You are logged in.";
- // Set up our auto log out functionality.
- resetAutoLogOut();
- }
- public function doLogOut() :void
- {
- message.text = "You are logged out.";
- loginPrompt.visible = true;
- }
- public function handleUserActivity(event:Event) :void
- {
- // Reset the timer whenever use activity is detected.
- resetAutoLogOut();
- }
- public function resetAutoLogOut() :void
- {
- // Clear old timeout.
- clearTimeout(activityTimeoutId);
- // Set up a timeout to trigger the log out function
- // after 30 seconds.
- var delay:Number = 30 * 1000;
- activityTimeoutId = setTimeout(doLogOut, delay);
- }
- ]]>
- </mx:Script>
- <mx:Label id="message" horizontalCenter="0" text="You are logged out." />
- <mx:Panel id="loginPrompt" title="Log In" horizontalCenter="0"
- top="20" horizontalAlign="right">
- <mx:Form>
- <mx:FormItem label="Username:">
- <mx:TextInput id="userNameInput" />
- </mx:FormItem>
- <mx:FormItem label="Password">
- <mx:TextInput id="passwordInput" displayAsPassword="true" />
- </mx:FormItem>
- </mx:Form>
- <mx:ControlBar horizontalAlign="center">
- <mx:Button id="submitButton" label="Submit" click="doLogIn()" />
- </mx:ControlBar>
- </mx:Panel>
- </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
Posted on Wednesday, 21st January 2009 by admin
Tags: Flex, MouseEvent, setTimeout, セッション, ログアウト
Posted in Flex | Comments (0) | 3,200 views
