lavkach3
188 строк · 5.2 Кб
1/*
2* jQuery Idle Timeout 1.2
3* Copyright (c) 2011 Eric Hynds
4*
5* http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
6*
7* Depends:
8* - jQuery 1.4.2+
9* - jQuery Idle Timer (by Paul Irish, http://paulirish.com/2009/jquery-idletimer-plugin/)
10*
11* Dual licensed under the MIT and GPL licenses:
12* http://www.opensource.org/licenses/mit-license.php
13* http://www.gnu.org/licenses/gpl.html
14*
15*/
16
17(function ($, win) {18var idleTimeout = {19init: function (element, resume, options) {20var self = this,21elem;22
23this.warning = elem = $(element);24this.resume = $(resume);25this.options = options;26this.countdownOpen = false;27this.failedRequests = options.failedRequests;28this._startTimer();29this.title = document.title;30
31// expose obj to data cache so peeps can call internal methods32$.data(elem[0], "idletimeout", this);33
34// start the idle timer35$.idleTimer(options.idleAfter * 1000);36
37// once the user becomes idle38$(document).bind("idle.idleTimer", function () {39// if the user is idle and a countdown isn't already running40if ($.data(document, "idleTimer") === "idle" && !self.countdownOpen) {41self._stopTimer();42self.countdownOpen = true;43self._idle();44}45});46
47// bind continue link48this.resume.bind("click", function (e) {49e.preventDefault();50
51win.clearInterval(self.countdown); // stop the countdown52self.countdownOpen = false; // stop countdown53self._startTimer(); // start up the timer again54self._keepAlive(false); // ping server55options.onResume.call(self.warning); // call the resume callback56});57},58
59_idle: function () {60var self = this,61options = this.options,62warning = this.warning[0],63counter = options.warningLength;64
65// fire the onIdle function66options.onIdle.call(warning);67
68// set inital value in the countdown placeholder69options.onCountdown.call(warning, counter);70
71// create a timer that runs every second72this.countdown = win.setInterval(function () {73if (--counter === 0) {74window.clearInterval(self.countdown);75options.onTimeout.call(warning);76} else {77options.onCountdown.call(warning, counter);78document.title =79options.titleMessage.replace("%s", counter) + self.title;80}81}, 1000);82},83
84_startTimer: function () {85var self = this;86
87this.timer = win.setTimeout(function () {88self._keepAlive();89}, this.options.pollingInterval * 1000);90},91
92_stopTimer: function () {93// reset the failed requests counter94this.failedRequests = this.options.failedRequests;95win.clearTimeout(this.timer);96},97
98_keepAlive: function (recurse) {99var self = this,100options = this.options;101
102//Reset the title to what it was.103document.title = self.title;104
105// assume a startTimer/keepAlive loop unless told otherwise106if (typeof recurse === "undefined") {107recurse = true;108}109
110// if too many requests failed, abort111if (!this.failedRequests) {112this._stopTimer();113options.onAbort.call(this.warning[0]);114return;115}116
117$.ajax({118timeout: options.AJAXTimeout,119url: options.keepAliveURL,120error: function () {121self.failedRequests--;122},123success: function (response) {124if ($.trim(response) !== options.serverResponseEquals) {125self.failedRequests--;126}127},128complete: function () {129if (recurse) {130self._startTimer();131}132},133});134},135};136
137// expose138$.idleTimeout = function (element, resume, options) {139idleTimeout.init(element, resume, $.extend($.idleTimeout.options, options));140return this;141};142
143// options144$.idleTimeout.options = {145// number of seconds after user is idle to show the warning146warningLength: 30,147
148// url to call to keep the session alive while the user is active149keepAliveURL: "",150
151// the response from keepAliveURL must equal this text:152serverResponseEquals: "OK",153
154// user is considered idle after this many seconds. 10 minutes default155idleAfter: 600,156
157// a polling request will be sent to the server every X seconds158pollingInterval: 60,159
160// number of failed polling requests until we abort this script161failedRequests: 5,162
163// the $.ajax timeout in MILLISECONDS!164AJAXTimeout: 250,165
166// %s will be replaced by the counter value167titleMessage: "Warning: %s seconds until log out | ",168
169/*170Callbacks
171"this" refers to the element found by the first selector passed to $.idleTimeout.
172*/
173// callback to fire when the session times out174onTimeout: $.noop,175
176// fires when the user becomes idle177onIdle: $.noop,178
179// fires during each second of warningLength180onCountdown: $.noop,181
182// fires when the user resumes the session183onResume: $.noop,184
185// callback to fire when the script is aborted due to too many failed requests186onAbort: $.noop,187};188})(jQuery, window);189