lavkach3

Форк
0
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) {
18
  var idleTimeout = {
19
    init: function (element, resume, options) {
20
      var self = this,
21
        elem;
22

23
      this.warning = elem = $(element);
24
      this.resume = $(resume);
25
      this.options = options;
26
      this.countdownOpen = false;
27
      this.failedRequests = options.failedRequests;
28
      this._startTimer();
29
      this.title = document.title;
30

31
      // expose obj to data cache so peeps can call internal methods
32
      $.data(elem[0], "idletimeout", this);
33

34
      // start the idle timer
35
      $.idleTimer(options.idleAfter * 1000);
36

37
      // once the user becomes idle
38
      $(document).bind("idle.idleTimer", function () {
39
        // if the user is idle and a countdown isn't already running
40
        if ($.data(document, "idleTimer") === "idle" && !self.countdownOpen) {
41
          self._stopTimer();
42
          self.countdownOpen = true;
43
          self._idle();
44
        }
45
      });
46

47
      // bind continue link
48
      this.resume.bind("click", function (e) {
49
        e.preventDefault();
50

51
        win.clearInterval(self.countdown); // stop the countdown
52
        self.countdownOpen = false; // stop countdown
53
        self._startTimer(); // start up the timer again
54
        self._keepAlive(false); // ping server
55
        options.onResume.call(self.warning); // call the resume callback
56
      });
57
    },
58

59
    _idle: function () {
60
      var self = this,
61
        options = this.options,
62
        warning = this.warning[0],
63
        counter = options.warningLength;
64

65
      // fire the onIdle function
66
      options.onIdle.call(warning);
67

68
      // set inital value in the countdown placeholder
69
      options.onCountdown.call(warning, counter);
70

71
      // create a timer that runs every second
72
      this.countdown = win.setInterval(function () {
73
        if (--counter === 0) {
74
          window.clearInterval(self.countdown);
75
          options.onTimeout.call(warning);
76
        } else {
77
          options.onCountdown.call(warning, counter);
78
          document.title =
79
            options.titleMessage.replace("%s", counter) + self.title;
80
        }
81
      }, 1000);
82
    },
83

84
    _startTimer: function () {
85
      var self = this;
86

87
      this.timer = win.setTimeout(function () {
88
        self._keepAlive();
89
      }, this.options.pollingInterval * 1000);
90
    },
91

92
    _stopTimer: function () {
93
      // reset the failed requests counter
94
      this.failedRequests = this.options.failedRequests;
95
      win.clearTimeout(this.timer);
96
    },
97

98
    _keepAlive: function (recurse) {
99
      var self = this,
100
        options = this.options;
101

102
      //Reset the title to what it was.
103
      document.title = self.title;
104

105
      // assume a startTimer/keepAlive loop unless told otherwise
106
      if (typeof recurse === "undefined") {
107
        recurse = true;
108
      }
109

110
      // if too many requests failed, abort
111
      if (!this.failedRequests) {
112
        this._stopTimer();
113
        options.onAbort.call(this.warning[0]);
114
        return;
115
      }
116

117
      $.ajax({
118
        timeout: options.AJAXTimeout,
119
        url: options.keepAliveURL,
120
        error: function () {
121
          self.failedRequests--;
122
        },
123
        success: function (response) {
124
          if ($.trim(response) !== options.serverResponseEquals) {
125
            self.failedRequests--;
126
          }
127
        },
128
        complete: function () {
129
          if (recurse) {
130
            self._startTimer();
131
          }
132
        },
133
      });
134
    },
135
  };
136

137
  // expose
138
  $.idleTimeout = function (element, resume, options) {
139
    idleTimeout.init(element, resume, $.extend($.idleTimeout.options, options));
140
    return this;
141
  };
142

143
  // options
144
  $.idleTimeout.options = {
145
    // number of seconds after user is idle to show the warning
146
    warningLength: 30,
147

148
    // url to call to keep the session alive while the user is active
149
    keepAliveURL: "",
150

151
    // the response from keepAliveURL must equal this text:
152
    serverResponseEquals: "OK",
153

154
    // user is considered idle after this many seconds.  10 minutes default
155
    idleAfter: 600,
156

157
    // a polling request will be sent to the server every X seconds
158
    pollingInterval: 60,
159

160
    // number of failed polling requests until we abort this script
161
    failedRequests: 5,
162

163
    // the $.ajax timeout in MILLISECONDS!
164
    AJAXTimeout: 250,
165

166
    // %s will be replaced by the counter value
167
    titleMessage: "Warning: %s seconds until log out | ",
168

169
    /*
170
			Callbacks
171
			"this" refers to the element found by the first selector passed to $.idleTimeout.
172
		*/
173
    // callback to fire when the session times out
174
    onTimeout: $.noop,
175

176
    // fires when the user becomes idle
177
    onIdle: $.noop,
178

179
    // fires during each second of warningLength
180
    onCountdown: $.noop,
181

182
    // fires when the user resumes the session
183
    onResume: $.noop,
184

185
    // callback to fire when the script is aborted due to too many failed requests
186
    onAbort: $.noop,
187
  };
188
})(jQuery, window);
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.