1

I have this code embedded in my file for managing session time out. This was referred from http://www.fairwaytech.com/2012/01/handling-session-timeout-gracefully/

I want to call SessionManager.extend() for all the ajax request complete. So i can automatically refresh my session manager time.

This is what i tried

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

Getting an error that SessionManager object not found. How do we call this?

Below is the library code taken from that site

$(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();
});
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120

1 Answers1

1

Remove the var prefix from SessionManager.

Bit of info here about scope, http://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx

Alex
  • 2,102
  • 1
  • 13
  • 16
  • Your suggestion will cause problems in "strict" ES5 JavaScript environments. It's erroneous to refer to global symbols in assignments like that. It'd be better to change it to an explicit global assignment: `window.SessionManager = ...` – Pointy Feb 04 '13 at 15:13
  • @Pointy, You are correct. Is there any way other than assigning this SessionManager in global window scope and retrieve? Something like a public helper method in SessionManager = function () that gives the instance to work with. – Murali Murugesan Feb 08 '13 at 06:44
  • @Murali the way that code is written deliberately hides the SessionManager function. The outer anonymous function that does the work returns nothing, but that could be changed so that it returned a reference to SessionManager. – Pointy Feb 08 '13 at 13:40