lavkach3

Форк
0
130 строк · 4.3 Кб
1
/*
2
 * jQuery idleTimer plugin
3
 * version 0.8.092209
4
 * by Paul Irish. 
5
 *   http://github.com/paulirish/yui-misc/tree/
6
 * MIT license
7
 
8
 * adapted from YUI idle timer by nzakas:
9
 *   http://github.com/nzakas/yui-misc/
10
 
11
 
12
 * Copyright (c) 2009 Nicholas C. Zakas
13
 * 
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 * 
21
 * The above copyright notice and this permission notice shall be included in
22
 * all copies or substantial portions of the Software.
23
 * 
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32

33
(function ($) {
34
  $.idleTimer = function f(newTimeout) {
35
    //$.idleTimer.tId = -1     //timeout ID
36

37
    var idle = false, //indicates if the user is idle
38
      enabled = true, //indicates if the idle timer is enabled
39
      timeout = 30000, //the amount of time (ms) before the user is considered idle
40
      events = "mousemove keydown DOMMouseScroll mousewheel mousedown", // activity is one of these events
41
      //f.olddate = undefined, // olddate used for getElapsedTime. stored on the function
42

43
      /* (intentionally not documented)
44
       * Toggles the idle state and fires an appropriate event.
45
       * @return {void}
46
       */
47
      toggleIdleState = function () {
48
        //toggle the state
49
        idle = !idle;
50

51
        // reset timeout counter
52
        f.olddate = +new Date();
53

54
        //fire appropriate event
55
        $(document).trigger(
56
          $.data(document, "idleTimer", idle ? "idle" : "active") + ".idleTimer"
57
        );
58
      },
59
      /**
60
       * Stops the idle timer. This removes appropriate event handlers
61
       * and cancels any pending timeouts.
62
       * @return {void}
63
       * @method stop
64
       * @static
65
       */
66
      stop = function () {
67
        //set to disabled
68
        enabled = false;
69

70
        //clear any pending timeouts
71
        clearTimeout($.idleTimer.tId);
72

73
        //detach the event handlers
74
        $(document).unbind(".idleTimer");
75
      },
76
      /* (intentionally not documented)
77
       * Handles a user event indicating that the user isn't idle.
78
       * @param {Event} event A DOM2-normalized event object.
79
       * @return {void}
80
       */
81
      handleUserEvent = function () {
82
        //clear any existing timeout
83
        clearTimeout($.idleTimer.tId);
84

85
        //if the idle timer is enabled
86
        if (enabled) {
87
          //if it's idle, that means the user is no longer idle
88
          if (idle) {
89
            toggleIdleState();
90
          }
91

92
          //set a new timeout
93
          $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
94
        }
95
      };
96

97
    /**
98
     * Starts the idle timer. This adds appropriate event handlers
99
     * and starts the first timeout.
100
     * @param {int} newTimeout (Optional) A new value for the timeout period in ms.
101
     * @return {void}
102
     * @method $.idleTimer
103
     * @static
104
     */
105

106
    f.olddate = f.olddate || +new Date();
107

108
    //assign a new timeout if necessary
109
    if (typeof newTimeout == "number") {
110
      timeout = newTimeout;
111
    } else if (newTimeout === "destroy") {
112
      stop();
113
      return this;
114
    } else if (newTimeout === "getElapsedTime") {
115
      return +new Date() - f.olddate;
116
    }
117

118
    //assign appropriate event handlers
119
    $(document).bind(
120
      $.trim((events + " ").split(" ").join(".idleTimer ")),
121
      handleUserEvent
122
    );
123

124
    //set a timeout to toggle state
125
    $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
126

127
    // assume the user is active for the first x seconds.
128
    $.data(document, "idleTimer", "active");
129
  }; // end of $.idleTimer()
130
})(jQuery);
131

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

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

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

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