jquery.idletimer.js 4.4 KB

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