lavkach3
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 ID36
37var idle = false, //indicates if the user is idle38enabled = true, //indicates if the idle timer is enabled39timeout = 30000, //the amount of time (ms) before the user is considered idle40events = "mousemove keydown DOMMouseScroll mousewheel mousedown", // activity is one of these events41//f.olddate = undefined, // olddate used for getElapsedTime. stored on the function42
43/* (intentionally not documented)44* Toggles the idle state and fires an appropriate event.
45* @return {void}
46*/
47toggleIdleState = function () {48//toggle the state49idle = !idle;50
51// reset timeout counter52f.olddate = +new Date();53
54//fire appropriate event55$(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*/
66stop = function () {67//set to disabled68enabled = false;69
70//clear any pending timeouts71clearTimeout($.idleTimer.tId);72
73//detach the event handlers74$(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*/
81handleUserEvent = function () {82//clear any existing timeout83clearTimeout($.idleTimer.tId);84
85//if the idle timer is enabled86if (enabled) {87//if it's idle, that means the user is no longer idle88if (idle) {89toggleIdleState();90}91
92//set a new timeout93$.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
106f.olddate = f.olddate || +new Date();107
108//assign a new timeout if necessary109if (typeof newTimeout == "number") {110timeout = newTimeout;111} else if (newTimeout === "destroy") {112stop();113return this;114} else if (newTimeout === "getElapsedTime") {115return +new Date() - f.olddate;116}117
118//assign appropriate event handlers119$(document).bind(120$.trim((events + " ").split(" ").join(".idleTimer ")),121handleUserEvent
122);123
124//set a timeout to toggle state125$.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