jQuery-sessionTimeout icon indicating copy to clipboard operation
jQuery-sessionTimeout copied to clipboard

A jQuery plugin which prevents server side sessions from timing out

jQuery Session Timeout

Prevents lost data due to session timeouts, by provivideing a user interface which alows users to refresh thier session before it expires

Getting Started

Download the production version or the development version.

In your web page:

<script src="jquery.js"></script>
<script src="dist/jquery.sessiontimeout.min.js"></script>
<script>
jQuery(document).ready(function(){
    $.fn.sessionTimeout(); 
});
</script>

Usage

The sessionTimeout plugin is called as a jQuery utility function

// automatically renew the users session every 30 minuets by default
$.fn.sessionTimeout();

Pass options into the plugin as an object. The following sample will display a JavaScript alert message 10 seconds before the user session expires. This sample also configures the session duration to last 30 seconds (30,000 ms).

jQuery(document).ready(function(){
    $.fn.sessionTimeout({
        timeout : 30000,
        promptfor : 10000,
        beforetimeout : function(){ 
            alert("do something!");
        } 
    }); 
});

Automatically renew the user session every 30 seconds

jQuery(document).ready(function(){
    $.fn.sessionTimeout({
        autoping: true,
        timeout : 30000
    }); 
});

Options

  • autoping: true (boolean) : automaticaly continues the users session
  • timeout: when the session will timeout
  • resource: "spacer.jpg" : url to an image or webpage (aspx, php, cfm, jsp, etc.) URL
  • promptfor: number of ms to prompt user
  • beforetimeout: function callback when the prompt begins
  • ontimeout: function callback when the session expires

Methods

$.fn.sessionTimeout("printLog"); // returns an array of logged plugin events
$.fn.sessionTimeout("ping"); // loads the uncahcable image specified in the Options img from the server 
$.fn.sessionTimeout("durration"); // returns servers durration in ms 
$.fn.sessionTimeout("elapsed"); // returns time session has been open in ms 
$.fn.sessionTimeout("remaining"); // returns time left until session expires in ms
$.fn.sessionTimeout("destroy"); // removes the plugin from the page and cleans up referneces

Events

$(document).bind("create.sessionTimeout", function(event, version));
$(document).bind("ping.sessionTimeout");
$(document).bind("prompt.sessionTimeout");
$(document).bind("destroy.sessionTimeout");
$(document).bind("expired.sessionTimeout");
$(document).bind("logEvent.sessionTimeout");

For example usage view examples/demo.html For unit tests view test/index.html

Examples

Basic Demo Jquery UI Demo

Release History

(Nothing yet)

License

Copyright (c) 2012 Asa Baylus
Licensed under the MIT, GPL licenses.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Important notes

Please don't edit files in the dist subdirectory as they are generated via grunt. You'll find source code in the src subdirectory!

While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html unit test file(s) in actual browsers.

Installing grunt

This assumes you have node.js and npm installed already.

  1. Test that grunt is installed globally by running grunt --version at the command-line.
  2. If grunt isn't installed globally, run npm install -g grunt to install the latest version. You may need to run sudo npm install -g grunt.
  3. From the root directory of this project, run npm install to install the project's dependencies.

Installing PhantomJS

In order for the qunit task to work properly, PhantomJS must be installed and in the system PATH (if you can run "phantomjs" at the command line, this task should work).

Unfortunately, PhantomJS cannot be installed automatically via npm or grunt, so you need to install it yourself. There are a number of ways to install PhantomJS.

Note that the phantomjs executable needs to be in the system PATH for grunt to see it.