1/* updated to fix Chrome setTimeout issue by Zaid Zawaideh */ 2 3 // API available in <= v0.8 4 /******************************* 5 6 // idleTimer() takes an optional argument that defines the idle timeout 7 // timeout is in milliseconds; defaults to 30000 8 $.idleTimer(10000); 9 10 11 $(document).bind("idle.idleTimer", function(){ 12 // function you want to fire when the user goes idle 13 }); 14 15 16 $(document).bind("active.idleTimer", function(){ 17 // function you want to fire when the user becomes active again 18 }); 19 20 // pass the string 'destroy' to stop the timer 21 $.idleTimer('destroy'); 22 23 // you can query if the user is idle or not with data() 24 $.data(document,'idleTimer'); // 'idle' or 'active' 25 26 // you can get time elapsed since user when idle/active 27 $.idleTimer('getElapsedTime'); // time since state change in ms 28 29 ********/ 30 31 32 33 // API available in >= v0.9 34 /************************* 35 36 // bind to specific elements, allows for multiple timer instances 37 $(elem).idleTimer(timeout|'destroy'|'getElapsedTime'); 38 $.data(elem,'idleTimer'); // 'idle' or 'active' 39 40 // if you're using the old $.idleTimer api, you should not do $(document).idleTimer(...) 41 42 // element bound timers will only watch for events inside of them. 43 // you may just want page-level activity, in which case you may set up 44 // your timers on document, document.documentElement, and document.body 45 46 // You can optionally provide a second argument to override certain options. 47 // Here are the defaults, so you can omit any or all of them. 48 $(elem).idleTimer(timeout, { 49 startImmediately: true, //starts a timeout as soon as the timer is set up; otherwise it waits for the first event. 50 idle: false, //indicates if the user is idle 51 enabled: true, //indicates if the idle timer is enabled 52 events: 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove' // activity is one of these events 53 }); 54 55 ********/ 56 57(function($){ 58 59$.idleTimer = function(newTimeout, elem, opts){ 60 61 // defaults that are to be stored as instance props on the elem 62 63 opts = $.extend({ 64 startImmediately: true, //starts a timeout as soon as the timer is set up 65 idle: false, //indicates if the user is idle 66 enabled: true, //indicates if the idle timer is enabled 67 timeout: 30000, //the amount of time (ms) before the user is considered idle 68 events: 'mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove' // activity is one of these events 69 }, opts); 70 71 72 elem = elem || document; 73 74 /* (intentionally not documented) 75 * Toggles the idle state and fires an appropriate event. 76 * @return {void} 77 */ 78 var toggleIdleState = function(myelem){ 79 80 // curse you, mozilla setTimeout lateness bug! 81 if (typeof myelem === 'number'){ 82 myelem = undefined; 83 } 84 85 var obj = $.data(myelem || elem,'idleTimerObj'); 86 87 //toggle the state 88 obj.idle = !obj.idle; 89 90 // reset timeout 91 var elapsed = (+new Date()) - obj.olddate; 92 obj.olddate = +new Date(); 93 94 // handle Chrome always triggering idle after js alert or comfirm popup 95 if (obj.idle && (elapsed < opts.timeout)) { 96 obj.idle = false; 97 clearTimeout($.idleTimer.tId); 98 if (opts.enabled) 99 $.idleTimer.tId = setTimeout(toggleIdleState, opts.timeout); 100 return; 101 } 102 103 //fire appropriate event 104 105 // create a custom event, but first, store the new state on the element 106 // and then append that string to a namespace 107 var event = jQuery.Event( $.data(elem,'idleTimer', obj.idle ? "idle" : "active" ) + '.idleTimer' ); 108 109 // we do want this to bubble, at least as a temporary fix for jQuery 1.7 110 // event.stopPropagation(); 111 $(elem).trigger(event); 112 }, 113 114 /** 115 * Stops the idle timer. This removes appropriate event handlers 116 * and cancels any pending timeouts. 117 * @return {void} 118 * @method stop 119 * @static 120 */ 121 stop = function(elem){ 122 123 var obj = $.data(elem,'idleTimerObj') || {}; 124 125 //set to disabled 126 obj.enabled = false; 127
128 //clear any pending timeouts 129 clearTimeout(obj.tId); 130 131 //detach the event handlers 132 $(elem).off('.idleTimer'); 133 }, 134 135 136 /* (intentionally not documented) 137 * Handles a user event indicating that the user isn't idle. 138 * @param {Event} event A DOM2-normalized event object. 139 * @return {void} 140 */ 141 handleUserEvent = function(){ 142 143 var obj = $.data(this,'idleTimerObj'); 144 145 //clear any existing timeout 146 clearTimeout(obj.tId); 147 148 149 150 //if the idle timer is enabled 151 if (obj.enabled){ 152 153 154 //if it's idle, that means the user is no longer idle 155 if (obj.idle){ 156 toggleIdleState(this); 157 } 158 159 //set a new timeout 160 obj.tId = setTimeout(toggleIdleState, obj.timeout); 161 162 } 163 }; 164 165 166 /** 167 * Starts the idle timer. This adds appropriate event handlers 168 * and starts the first timeout. 169 * @param {int} newTimeout (Optional) A new value for the timeout period in ms. 170 * @return {void} 171 * @method $.idleTimer 172 * @static 173 */ 174 175 176 var obj = $.data(elem,'idleTimerObj') || {}; 177 178 obj.olddate = obj.olddate || +new Date(); 179 180 //assign a new timeout if necessary 181 if (typeof newTimeout === "number"){ 182 opts.timeout = newTimeout; 183 } else if (newTimeout === 'destroy') { 184 stop(elem); 185 return this; 186 } else if (newTimeout === 'getElapsedTime'){ 187 return (+new Date()) - obj.olddate; 188 } 189 190 //assign appropriate event handlers 191 $(elem).on($.trim((opts.events+' ').split(' ').join('.idleTimer ')),handleUserEvent); 192 193 194 obj.idle = opts.idle; 195 obj.enabled = opts.enabled; 196 obj.timeout = opts.timeout; 197 198 199 //set a timeout to toggle state. May wish to omit this in some situations 200 if (opts.startImmediately) { 201 obj.tId = setTimeout(toggleIdleState, obj.timeout); 202 } 203 204 // assume the user is active for the first x seconds. 205 $.data(elem,'idleTimer',"active"); 206 207 // store our instance on the object 208 $.data(elem,'idleTimerObj',obj); 209 210 211 212}; // end of $.idleTimer() 213 214 215// v0.9 API for defining multiple timers. 216$.fn.idleTimer = function(newTimeout,opts){ 217 // Allow omission of opts for backward compatibility 218 if (!opts) { 219 opts = {}; 220 } 221 222 if(this[0]){ 223 $.idleTimer(newTimeout,this[0],opts); 224 } 225 226 return this; 227}; 228 229 230})(jQuery);// JavaScript Document
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.