idle-timer.1.0.0.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*! Idle Timer - v1.0.0 - 2014-03-10
  2. * https://github.com/thorst/jquery-idletimer
  3. * Copyright (c) 2014 Paul Irish; Licensed MIT */
  4. /*
  5. mousewheel (deprecated) -> IE6.0, Chrome, Opera, Safari
  6. DOMMouseScroll (deprecated) -> Firefox 1.0
  7. wheel (standard) -> Chrome 31, Firefox 17, IE9, Firefox Mobile 17.0
  8. //No need to use, use DOMMouseScroll
  9. MozMousePixelScroll -> Firefox 3.5, Firefox Mobile 1.0
  10. //Events
  11. WheelEvent -> see wheel
  12. MouseWheelEvent -> see mousewheel
  13. MouseScrollEvent -> Firefox 3.5, Firefox Mobile 1.0
  14. */
  15. (function ($) {
  16. $.idleTimer = function (firstParam, elem) {
  17. var opts;
  18. if ( typeof firstParam === "object" ) {
  19. opts = firstParam;
  20. firstParam = null;
  21. } else if (typeof firstParam === "number") {
  22. opts = { timeout: firstParam };
  23. firstParam = null;
  24. }
  25. // element to watch
  26. elem = elem || document;
  27. // defaults that are to be stored as instance props on the elem
  28. opts = $.extend({
  29. idle: false, // indicates if the user is idle
  30. timeout: 30000, // the amount of time (ms) before the user is considered idle
  31. events: "mousemove keydown wheel DOMMouseScroll mousewheel mousedown touchstart touchmove MSPointerDown MSPointerMove" // define active events
  32. }, opts);
  33. var jqElem = $(elem),
  34. obj = jqElem.data("idleTimerObj") || {},
  35. /* (intentionally not documented)
  36. * Toggles the idle state and fires an appropriate event.
  37. * @return {void}
  38. */
  39. toggleIdleState = function (e) {
  40. var obj = $.data(elem, "idleTimerObj") || {};
  41. // toggle the state
  42. obj.idle = !obj.idle;
  43. // store toggle state date time
  44. obj.olddate = +new Date();
  45. // create a custom event, with state and name space
  46. var event = $.Event((obj.idle ? "idle" : "active") + ".idleTimer");
  47. // trigger event on object with elem and copy of obj
  48. $(elem).trigger(event, [elem, $.extend({}, obj), e]);
  49. },
  50. /**
  51. * Handle event triggers
  52. * @return {void}
  53. * @method event
  54. * @static
  55. */
  56. handleEvent = function (e) {
  57. var obj = $.data(elem, "idleTimerObj") || {};
  58. // this is already paused, ignore events for now
  59. if (obj.remaining != null) { return; }
  60. /*
  61. mousemove is kinda buggy, it can be triggered when it should be idle.
  62. Typically is happening between 115 - 150 milliseconds after idle triggered.
  63. @psyafter & @kaellis report "always triggered if using modal (jQuery ui, with overlay)"
  64. @thorst has similar issues on ios7 "after $.scrollTop() on text area"
  65. */
  66. if (e.type === "mousemove") {
  67. // if coord are same, it didn't move
  68. if (e.pageX === obj.pageX && e.pageY === obj.pageY) {
  69. return;
  70. }
  71. // if coord don't exist how could it move
  72. if (typeof e.pageX === "undefined" && typeof e.pageY === "undefined") {
  73. return;
  74. }
  75. // under 200 ms is hard to do, and you would have to stop, as continuous activity will bypass this
  76. var elapsed = (+new Date()) - obj.olddate;
  77. if (elapsed < 200) {
  78. return;
  79. }
  80. }
  81. // clear any existing timeout
  82. clearTimeout(obj.tId);
  83. // if the idle timer is enabled, flip
  84. if (obj.idle) {
  85. toggleIdleState(e);
  86. }
  87. // store when user was last active
  88. obj.lastActive = +new Date();
  89. // update mouse coord
  90. obj.pageX = e.pageX;
  91. obj.pageY = e.pageY;
  92. // set a new timeout
  93. obj.tId = setTimeout(toggleIdleState, obj.timeout);
  94. },
  95. /**
  96. * Restore initial settings and restart timer
  97. * @return {void}
  98. * @method reset
  99. * @static
  100. */
  101. reset = function () {
  102. var obj = $.data(elem, "idleTimerObj") || {};
  103. // reset settings
  104. obj.idle = obj.idleBackup;
  105. obj.olddate = +new Date();
  106. obj.lastActive = obj.olddate;
  107. obj.remaining = null;
  108. // reset Timers
  109. clearTimeout(obj.tId);
  110. if (!obj.idle) {
  111. obj.tId = setTimeout(toggleIdleState, obj.timeout);
  112. }
  113. },
  114. /**
  115. * Store remaining time, stop timer
  116. * You can pause from an idle OR active state
  117. * @return {void}
  118. * @method pause
  119. * @static
  120. */
  121. pause = function () {
  122. var obj = $.data(elem, "idleTimerObj") || {};
  123. // this is already paused
  124. if ( obj.remaining != null ) { return; }
  125. // define how much is left on the timer
  126. obj.remaining = obj.timeout - ((+new Date()) - obj.olddate);
  127. // clear any existing timeout
  128. clearTimeout(obj.tId);
  129. },
  130. /**
  131. * Start timer with remaining value
  132. * @return {void}
  133. * @method resume
  134. * @static
  135. */
  136. resume = function () {
  137. var obj = $.data(elem, "idleTimerObj") || {};
  138. // this isn't paused yet
  139. if ( obj.remaining == null ) { return; }
  140. // start timer
  141. if ( !obj.idle ) {
  142. obj.tId = setTimeout(toggleIdleState, obj.remaining);
  143. }
  144. // clear remaining
  145. obj.remaining = null;
  146. },
  147. /**
  148. * Stops the idle timer. This removes appropriate event handlers
  149. * and cancels any pending timeouts.
  150. * @return {void}
  151. * @method destroy
  152. * @static
  153. */
  154. destroy = function () {
  155. var obj = $.data(elem, "idleTimerObj") || {};
  156. //clear any pending timeouts
  157. clearTimeout(obj.tId);
  158. //Remove data
  159. jqElem.removeData("idleTimerObj");
  160. //detach the event handlers
  161. jqElem.off("._idleTimer");
  162. },
  163. /**
  164. * Returns the time until becoming idle
  165. * @return {number}
  166. * @method remainingtime
  167. * @static
  168. */
  169. remainingtime = function () {
  170. var obj = $.data(elem, "idleTimerObj") || {};
  171. //If idle there is no time remaining
  172. if ( obj.idle ) { return 0; }
  173. //If its paused just return that
  174. if ( obj.remaining != null ) { return obj.remaining; }
  175. //Determine remaining, if negative idle didn't finish flipping, just return 0
  176. var remaining = obj.timeout - ((+new Date()) - obj.lastActive);
  177. if (remaining < 0) { remaining = 0; }
  178. //If this is paused return that number, else return current remaining
  179. return remaining;
  180. };
  181. // determine which function to call
  182. if (firstParam === null && typeof obj.idle !== "undefined") {
  183. // they think they want to init, but it already is, just reset
  184. reset();
  185. return jqElem;
  186. } else if (firstParam === null) {
  187. // they want to init
  188. } else if (firstParam !== null && typeof obj.idle === "undefined") {
  189. // they want to do something, but it isnt init
  190. // not sure the best way to handle this
  191. return false;
  192. } else if (firstParam === "destroy") {
  193. destroy();
  194. return jqElem;
  195. } else if (firstParam === "pause") {
  196. pause();
  197. return jqElem;
  198. } else if (firstParam === "resume") {
  199. resume();
  200. return jqElem;
  201. } else if (firstParam === "reset") {
  202. reset();
  203. return jqElem;
  204. } else if (firstParam === "getRemainingTime") {
  205. return remainingtime();
  206. } else if (firstParam === "getElapsedTime") {
  207. return (+new Date()) - obj.olddate;
  208. } else if (firstParam === "getLastActiveTime") {
  209. return obj.lastActive;
  210. } else if (firstParam === "isIdle") {
  211. return obj.idle;
  212. }
  213. /* (intentionally not documented)
  214. * Handles a user event indicating that the user isn't idle. namespaced with internal idleTimer
  215. * @param {Event} event A DOM2-normalized event object.
  216. * @return {void}
  217. */
  218. jqElem.on($.trim((opts.events + " ").split(" ").join("._idleTimer ")), function (e) {
  219. handleEvent(e);
  220. });
  221. // Internal Object Properties, This isn't all necessary, but we
  222. // explicitly define all keys here so we know what we are working with
  223. obj = $.extend({}, {
  224. olddate : +new Date(), // the last time state changed
  225. lastActive: +new Date(), // the last time timer was active
  226. idle : opts.idle, // current state
  227. idleBackup : opts.idle, // backup of idle parameter since it gets modified
  228. timeout : opts.timeout, // the interval to change state
  229. remaining : null, // how long until state changes
  230. tId : null, // the idle timer setTimeout
  231. pageX : null, // used to store the mouse coord
  232. pageY : null
  233. });
  234. // set a timeout to toggle state. May wish to omit this in some situations
  235. if (!obj.idle) {
  236. obj.tId = setTimeout(toggleIdleState, obj.timeout);
  237. }
  238. // store our instance on the object
  239. $.data(elem, "idleTimerObj", obj);
  240. return jqElem;
  241. };
  242. // This allows binding to element
  243. $.fn.idleTimer = function (firstParam) {
  244. if (this[0]) {
  245. return $.idleTimer(firstParam, this[0]);
  246. }
  247. return this;
  248. };
  249. })(jQuery);