Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          /**
          ?* Author : Larry Li
          ?* Date : 2013-12-5
          ?* Email : larry.li@aicent.com
          ?*/
          var Session = function() {
          ?? ?var defaults = {
          ?? ??? ??? ?title??????? : 'Session Notification',
          ?? ??? ??? ?message????? : 'Your session is about to expire.',
          ?? ??? ??? ?keepAliveUrl : '/admin/session/keep-alive',
          ?? ??? ??? ?redirUrl???? : '/account/timed-out',
          ?? ??? ??? ?logoutUrl??? : '/account/logout',
          ?? ??? ??? ?warnAfter??? : 900000, // 15 minutes
          ?? ??? ??? ?redirAfter?? : 1200000 // 20 minutes
          ?? ?};
          ?? ?
          ?? ?var o = defaults, dialogTimer, redirTimer;
          ?? ?
          ?? ?var controlRedirTimer = function(action) {
          ?? ??? ?switch(action) {
          ?? ??? ??? ?case 'start':
          ?? ??? ??? ??? ?// Dialog has been shown, if no action taken during redir period, redirect
          ?? ??? ??? ??? ?redirTimer = setTimeout(function(){
          ?? ??? ??? ??? ??? ?window.location = o.redirUrl;
          ?? ??? ??? ??? ?}, o.redirAfter - o.warnAfter);
          ?? ??? ??? ??? ?break;
          ?? ?
          ?? ??? ??? ?case 'stop':
          ?? ??? ??? ??? ?clearTimeout(redirTimer);
          ?? ??? ??? ??? ?break;
          ?? ??? ??? ?case 'restart':
          ?? ??? ??? ??? ?clearTimeout(redirTimer);
          ?? ??? ??? ??? ?redirTimer = setTimeout(function(){
          ?? ??? ??? ??? ??? ?window.location = o.redirUrl;
          ?? ??? ??? ??? ?}, o.redirAfter - o.warnAfter);
          ?? ??? ??? ??? ?break;
          ?? ??? ?}
          ?? ?};
          ?? ?
          ?? ?var controlDialogTimer = function(action) {
          ?? ??? ?switch(action) {
          ?? ??? ??? ?case 'start':
          ?? ??? ??? ??? ?dialogTimer = setTimeout(function(){
          ?? ??? ??? ??? ??? ?$('#sessionTimeout-dialog').modal('show');
          ?? ??? ??? ??? ??? ?controlRedirTimer('start');
          ?? ??? ??? ??? ?}, o.warnAfter);
          ?? ??? ??? ??? ?break;
          ?? ??? ??? ?case 'stop':
          ?? ??? ??? ??? ?clearTimeout(dialogTimer);
          ?? ??? ??? ??? ?break;
          ?? ??? ??? ?case 'restart':
          ?? ??? ??? ??? ?clearTimeout(dialogTimer);
          ?? ??? ??? ??? ?dialogTimer = setTimeout(function(){
          ?? ??? ??? ??? ??? ?$('#sessionTimeout-dialog').modal('show');
          ?? ??? ??? ??? ??? ?controlRedirTimer('restart');
          ?? ??? ??? ??? ?}, o.warnAfter);
          ?? ??? ??? ??? ?break;
          ?? ??? ?}
          ?? ?};
          ?? ?
          ?? ?var doKeepAlive = function() {
          ?? ??? ?$.ajax({
          ?? ??? ??? ?type: 'POST',
          ?? ??? ??? ?url: o.keepAliveUrl,
          ?? ??? ??? ?success: function() {
          ?? ??? ??? ??? ?// Stop redirect timer and restart warning timer
          ?? ??? ??? ??? ?controlRedirTimer('restart');
          ?? ??? ??? ??? ?controlDialogTimer('restart');
          ?? ??? ??? ?}
          ?? ??? ?});
          ?? ?};
          ?? ?
          ?? ?return {
          ?? ??? ?sessionTimeout: function(options) {
          ?? ??? ??? ?if ( options ) { o = $.extend( defaults, options ); }
          ?? ??? ??? ?
          ?? ??? ??? ?var warning_dialog = '<div class="modal fade" id="sessionTimeout-dialog">'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ?<div class="modal-dialog modal-small">'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ?<div class="modal-content">'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ?<div class="modal-header">'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ??? ?<button id="_close" type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ??? ?<h4 class="modal-title">'+ o.title +'</h4>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ?</div>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ?<div class="modal-body">'+ o.message +'</div>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ?<div class="modal-footer">'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ??? ?<button id="sessionTimeout-dialog-logout" type="button" class="btn btn-default">Logout</button>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ??? ?<button id="sessionTimeout-dialog-keepalive" type="button" class="btn btn-primary" data-dismiss="modal">Stay Connected</button>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ??? ?</div>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ??? ?</div>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'?? ?</div>'
          ?? ??? ??? ??? ??? ??? ??? ??? ?+'</div>';
          ?? ??? ??? ?
          ?? ??? ??? ?$('body').append(warning_dialog);
          ?? ??? ??? ?
          ?? ??? ??? ?$('#sessionTimeout-dialog-logout').on('click', function () { window.location = o.logoutUrl; });
          ?? ??? ??? ?
          ?? ??? ??? ?$('#_close,#sessionTimeout-dialog-keepalive').click(function() {
          ?? ??? ??? ??? ?doKeepAlive();
          ?? ??? ??? ?});
          ?? ??? ??? ?// Begin warning period
          ?? ??? ??? ?controlDialogTimer('start');
          ?? ??? ?},
          ?? ??? ?
          ?? ??? ?sessionTimeoutKeepAlive: function() {
          ?? ??? ??? ?controlRedirTimer('restart');
          ?? ??? ??? ?controlDialogTimer('restart');
          ?? ??? ?}
          ?? ?};
          }();

          $(function() {
          ?? ?Session.sessionTimeout({
          ?? ??? ?title: 'Session Timeout Notification',
          ?? ??? ?message: 'Your session is about to expire.',
          ?? ??? ?keepAliveUrl: contextPath + '/admin/session/keep-alive',
          ?? ??? ?redirUrl: contextPath + '/account/login',
          ?? ??? ?logoutUrl: contextPath + '/account/logout',
          ?? ??? ?warnAfter: 1500000,
          ?? ??? ?redirAfter: 1780000
          ?? ?});
          ?? ?
          ?? ?$(document).ajaxComplete(function() {
          ?? ??? ?Session.sessionTimeoutKeepAlive();
          ?? ?});
          });
          posted on 2013-12-05 16:11 禮物 閱讀(1402) 評論(1)  編輯  收藏 所屬分類: javascriptAjax 、jquery 、Web前端

          評論

          # re: Ajax Session Timeout 超時 處理 2015-08-03 11:14 11
          sads  回復  更多評論
            

          主站蜘蛛池模板: 吉木乃县| 英山县| 锡林郭勒盟| 大庆市| 桦甸市| 贵港市| 江西省| 武隆县| 天长市| 京山县| 金华市| 金秀| 金溪县| 共和县| 天镇县| 二手房| 东方市| 婺源县| 怀宁县| 太仓市| 遂昌县| 宽城| 广安市| 阜阳市| 广灵县| 石泉县| 宿松县| 延安市| 五华县| 常山县| 金阳县| 黄梅县| 迁西县| 万载县| 成都市| 海淀区| 武川县| 南召县| 英德市| 都匀市| 哈巴河县|