在外面访问jQuery Closure函数

时间:2013-02-04 14:48:02

标签: javascript jquery jquery-ui closures

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

引用的
  

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

这是我试过的

<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();
});

1 个答案:

答案 0 :(得分:1)

从SessionManager中删除var前缀。

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