1

我将此代码嵌入到我的文件中,用于管理会话超时。这是来自http://www.fairwaytech.com/2012/01/handling-session-timeout-gracefully/

我想调用 SessionManager.extend() 来完成所有 ajax 请求。所以我可以自动刷新我的会话管理器时间。

这是我试过的

<script type="text/javascript">
    $(document).ajaxSuccess(function (event, xhr, settings) {
        if (xhr.status === 200) {
            SessionManager().extend();
        }
    });
</script>

收到未找到 SessionManager 对象的错误。我们怎么称呼它?

以下是取自该站点的库代码

$(function () { // Wrap it all in jQuery documentReady because we use jQuery UI Dialog
// HtmlHelpers Module
// Call by using HtmlHelpers.getQueryStringValue("myname");
var HtmlHelpers = function () {
    return {
        // Based on http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
        getQueryStringValue: function (name) {
            var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
            return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
        }
    };
} ();

// StringHelpers Module
// Call by using StringHelpers.padLeft("1", "000");
var StringHelpers = function () {
    return {
        // Pad string using padMask.  string '1' with padMask '000' will produce '001'.
        padLeft: function (string, padMask) {
            string = '' + string;
            return (padMask.substr(0, (padMask.length - string.length)) + string);
        }
    };
} ();

// SessionManager Module
var SessionManager = function () {
    // NOTE:  globalTimeoutPeriod is defined in _Layout.cshtml

    var sessionTimeoutSeconds = HtmlHelpers.getQueryStringValue('smt') || (globalTimeoutPeriod),
        countdownSeconds = HtmlHelpers.getQueryStringValue('smc') || 300,
        secondsBeforePrompt = sessionTimeoutSeconds - countdownSeconds,
        $dlg,
        displayCountdownIntervalId,
        promptToExtendSessionTimeoutId,
        originalTitle = document.title,
        count = countdownSeconds,
        extendSessionUrl = '/Session/Extend',
        expireSessionUrl = '/Session/Expire?returnUrl=' + location.pathname;

    var endSession = function () {
        $dlg.dialog('close');
        location.href = expireSessionUrl;
    };

    var displayCountdown = function () {
        var countdown = function () {
            var cd = new Date(count * 1000),
                minutes = cd.getUTCMinutes(),
                seconds = cd.getUTCSeconds(),
                minutesDisplay = minutes === 1 ? '1 minute ' : minutes === 0 ? '' : minutes + ' minutes ',
                secondsDisplay = seconds === 1 ? '1 second' : seconds + ' seconds',
                cdDisplay = minutesDisplay + secondsDisplay;

            document.title = 'Expire in ' +
                StringHelpers.padLeft(minutes, '00') + ':' +
                    StringHelpers.padLeft(seconds, '00');
            $('#sm-countdown').html(cdDisplay);
            if (count === 0) {
                document.title = 'Session Expired';
                endSession();
            }
            count--;
        };
        countdown();
        displayCountdownIntervalId = window.setInterval(countdown, 1000);
    };

    var promptToExtendSession = function () {
        $dlg = $('#sm-countdown-dialog')
            .dialog({
                title: 'Session Timeout Warning',
                height: 250,
                width: 350,
                bgiframe: true,
                modal: true,
                buttons: {
                    'Continue': function () {
                        $(this).dialog('close');
                        refreshSession();
                        document.title = originalTitle;
                    },
                    'Log Out': function () {
                        endSession(false);
                    }
                }
            });
        count = countdownSeconds;
        displayCountdown();
    };

    var refreshSession = function () {
        window.clearInterval(displayCountdownIntervalId);
        var img = new Image(1, 1);
        img.src = extendSessionUrl;
        window.clearTimeout(promptToExtendSessionTimeoutId);
        startSessionManager();
    };

    var startSessionManager = function () {
        promptToExtendSessionTimeoutId =
            window.setTimeout(promptToExtendSession, secondsBeforePrompt * 1000);
    };

    // Public Functions
    return {
        start: function () {
            startSessionManager();
        },

        extend: function () {
            refreshSession();
        }
    };
} ();

 SessionManager.start();
});
4

1 回答 1

1

从 SessionManager 中删除 var 前缀。

这里有一些关于范围的信息, http: //msdn.microsoft.com/en-us/library/ie/bzt2dkta (v=vs.94).aspx

于 2013-02-04T15:05:44.423 回答