jquery.idletimer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * adapted from YUI idle timer by nzakas:
  8. * http://github.com/nzakas/yui-misc/
  9. * Copyright (c) 2009 Nicholas C. Zakas
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. (function($){
  30. function SEPARATOR() { return " "; }
  31. function IDLE_TIMER() { return ".idleTimer"; }
  32. $.idleTimer = function f(newTimeout) {
  33. //$.idleTimer.tId = -1 //timeout ID
  34. var idle = false, //indicates if the user is idle
  35. enabled = true, //indicates if the idle timer is enabled
  36. timeout = 30000, //the amount of time (ms) before the user is considered idle
  37. events = "mousemove keydown DOMMouseScroll mousewheel mousedown", // activity is one of these events
  38. //f.olddate = undefined, // olddate used for getElapsedTime. stored on the function
  39. /* (intentionally not documented)
  40. * Toggles the idle state and fires an appropriate event.
  41. * @return {void}
  42. */
  43. toggleIdleState = function(){
  44. //toggle the state
  45. idle = !idle;
  46. // reset timeout counter
  47. f.olddate = +new Date;
  48. //fire appropriate event
  49. $(document).trigger( $.data(document,"idleTimer", idle ? "idle" : "active" ) + IDLE_TIMER());
  50. },
  51. /**
  52. * Stops the idle timer. This removes appropriate event handlers
  53. * and cancels any pending timeouts.
  54. * @return {void}
  55. * @method stop
  56. * @static
  57. */
  58. stop = function() {
  59. //set to disabled
  60. enabled = false;
  61. //clear any pending timeouts
  62. clearTimeout($.idleTimer.tId);
  63. //detach the event handlers
  64. $(document).unbind(IDLE_TIMER());
  65. },
  66. /* (intentionally not documented)
  67. * Handles a user event indicating that the user isn't idle.
  68. * @param {Event} event A DOM2-normalized event object.
  69. * @return {void}
  70. */
  71. handleUserEvent = function() {
  72. //clear any existing timeout
  73. clearTimeout($.idleTimer.tId);
  74. //if the idle timer is enabled
  75. if (enabled) {
  76. //if it's idle, that means the user is no longer idle
  77. if (idle) {
  78. toggleIdleState();
  79. }
  80. //set a new timeout
  81. $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
  82. }
  83. };
  84. /**
  85. * Starts the idle timer. This adds appropriate event handlers
  86. * and starts the first timeout.
  87. * @param {int} newTimeout (Optional) A new value for the timeout period in ms.
  88. * @return {void}
  89. * @method $.idleTimer
  90. * @static
  91. */
  92. f.olddate = f.olddate || +new Date;
  93. //assign a new timeout if necessary
  94. if (typeof newTimeout == "number"){
  95. timeout = newTimeout;
  96. } else if (newTimeout === "destroy") {
  97. stop();
  98. return this;
  99. } else if (newTimeout === "getElapsedTime"){
  100. return (+new Date) - f.olddate;
  101. }
  102. //assign appropriate event handlers
  103. $(document).bind($.trim((events + SEPARATOR()).split(SEPARATOR()).join(IDLE_TIMER() + SEPARATOR())),handleUserEvent);
  104. //set a timeout to toggle state
  105. $.idleTimer.tId = setTimeout(toggleIdleState, timeout);
  106. // assume the user is active for the first x seconds.
  107. $.data(document,"idleTimer","active");
  108. }; // end of $.idleTimer()
  109. })(jQuery);