提交 | 用户 | 时间
|
58d006
|
1 |
/* |
A |
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 |
|
|
35 |
$.idleTimer = function f(newTimeout){ |
|
36 |
|
|
37 |
//$.idleTimer.tId = -1 //timeout ID |
|
38 |
|
|
39 |
var idle = false, //indicates if the user is idle |
|
40 |
enabled = true, //indicates if the idle timer is enabled |
|
41 |
timeout = 30000, //the amount of time (ms) before the user is considered idle |
|
42 |
events = 'mousemove keydown DOMMouseScroll mousewheel mousedown', // activity is one of these events |
|
43 |
//f.olddate = undefined, // olddate used for getElapsedTime. stored on the function |
|
44 |
|
|
45 |
/* (intentionally not documented) |
|
46 |
* Toggles the idle state and fires an appropriate event. |
|
47 |
* @return {void} |
|
48 |
*/ |
|
49 |
toggleIdleState = function(){ |
|
50 |
|
|
51 |
//toggle the state |
|
52 |
idle = !idle; |
|
53 |
|
|
54 |
// reset timeout counter |
|
55 |
f.olddate = +new Date; |
|
56 |
|
|
57 |
//fire appropriate event |
|
58 |
$(document).trigger( $.data(document,'idleTimer', idle ? "idle" : "active" ) + '.idleTimer'); |
|
59 |
}, |
|
60 |
|
|
61 |
/** |
|
62 |
* Stops the idle timer. This removes appropriate event handlers |
|
63 |
* and cancels any pending timeouts. |
|
64 |
* @return {void} |
|
65 |
* @method stop |
|
66 |
* @static |
|
67 |
*/ |
|
68 |
stop = function(){ |
|
69 |
|
|
70 |
//set to disabled |
|
71 |
enabled = false; |
|
72 |
|
|
73 |
//clear any pending timeouts |
|
74 |
clearTimeout($.idleTimer.tId); |
|
75 |
|
|
76 |
//detach the event handlers |
|
77 |
$(document).unbind('.idleTimer'); |
|
78 |
}, |
|
79 |
|
|
80 |
|
|
81 |
/* (intentionally not documented) |
|
82 |
* Handles a user event indicating that the user isn't idle. |
|
83 |
* @param {Event} event A DOM2-normalized event object. |
|
84 |
* @return {void} |
|
85 |
*/ |
|
86 |
handleUserEvent = function(){ |
|
87 |
|
|
88 |
//clear any existing timeout |
|
89 |
clearTimeout($.idleTimer.tId); |
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
//if the idle timer is enabled |
|
94 |
if (enabled){ |
|
95 |
|
|
96 |
|
|
97 |
//if it's idle, that means the user is no longer idle |
|
98 |
if (idle){ |
|
99 |
toggleIdleState(); |
|
100 |
} |
|
101 |
|
|
102 |
//set a new timeout |
|
103 |
$.idleTimer.tId = setTimeout(toggleIdleState, timeout); |
|
104 |
|
|
105 |
} |
|
106 |
}; |
|
107 |
|
|
108 |
|
|
109 |
/** |
|
110 |
* Starts the idle timer. This adds appropriate event handlers |
|
111 |
* and starts the first timeout. |
|
112 |
* @param {int} newTimeout (Optional) A new value for the timeout period in ms. |
|
113 |
* @return {void} |
|
114 |
* @method $.idleTimer |
|
115 |
* @static |
|
116 |
*/ |
|
117 |
|
|
118 |
|
|
119 |
f.olddate = f.olddate || +new Date; |
|
120 |
|
|
121 |
//assign a new timeout if necessary |
|
122 |
if (typeof newTimeout == "number"){ |
|
123 |
timeout = newTimeout; |
|
124 |
} else if (newTimeout === 'destroy') { |
|
125 |
stop(); |
|
126 |
return this; |
|
127 |
} else if (newTimeout === 'getElapsedTime'){ |
|
128 |
return (+new Date) - f.olddate; |
|
129 |
} |
|
130 |
|
|
131 |
//assign appropriate event handlers |
|
132 |
$(document).bind($.trim((events+' ').split(' ').join('.idleTimer ')),handleUserEvent); |
|
133 |
|
|
134 |
|
|
135 |
//set a timeout to toggle state |
|
136 |
$.idleTimer.tId = setTimeout(toggleIdleState, timeout); |
|
137 |
|
|
138 |
// assume the user is active for the first x seconds. |
|
139 |
$.data(document,'idleTimer',"active"); |
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
}; // end of $.idleTimer() |
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
})(jQuery); |