jquery.idletimeout.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * jQuery Idle Timeout 1.2
  3. * Copyright (c) 2011 Eric Hynds
  4. *
  5. * http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
  6. *
  7. * Depends:
  8. * - jQuery 1.4.2+
  9. * - jQuery Idle Timer (by Paul Irish, http://paulirish.com/2009/jquery-idletimer-plugin/)
  10. *
  11. * Dual licensed under the MIT and GPL licenses:
  12. * http://www.opensource.org/licenses/mit-license.php
  13. * http://www.gnu.org/licenses/gpl.html
  14. *
  15. */
  16. (function($, win){
  17. var idleTimeout = {
  18. init: function( element, resume, options ){
  19. var self = this, elem;
  20. this.warning = elem = $(element);
  21. this.resume = $(resume);
  22. this.options = options;
  23. this.countdownOpen = false;
  24. this.failedRequests = options.failedRequests;
  25. this._startTimer();
  26. this.title = document.title;
  27. // expose obj to data cache so peeps can call internal methods
  28. $.data( elem[0], 'idletimeout', this );
  29. // start the idle timer
  30. $.idleTimer(options.idleAfter * 1000);
  31. // once the user becomes idle
  32. $(document).bind("idle.idleTimer", function(){
  33. // if the user is idle and a countdown isn't already running
  34. if( $.data(document, 'idleTimer') === 'idle' && !self.countdownOpen ){
  35. self._stopTimer();
  36. self.countdownOpen = true;
  37. self._idle();
  38. }
  39. });
  40. // bind continue link
  41. this.resume.bind("click", function(e){
  42. e.preventDefault();
  43. win.clearInterval(self.countdown); // stop the countdown
  44. self.countdownOpen = false; // stop countdown
  45. self._startTimer(); // start up the timer again
  46. self._keepAlive( false ); // ping server
  47. options.onResume.call( self.warning ); // call the resume callback
  48. });
  49. },
  50. _idle: function(){
  51. var self = this,
  52. options = this.options,
  53. warning = this.warning[0],
  54. counter = options.warningLength;
  55. // fire the onIdle function
  56. options.onIdle.call(warning);
  57. // set inital value in the countdown placeholder
  58. options.onCountdown.call(warning, counter);
  59. // create a timer that runs every second
  60. this.countdown = win.setInterval(function(){
  61. if(--counter === 0){
  62. window.clearInterval(self.countdown);
  63. options.onTimeout.call(warning);
  64. } else {
  65. options.onCountdown.call(warning, counter);
  66. document.title = options.titleMessage.replace('%s', counter) + self.title;
  67. }
  68. }, 1000);
  69. },
  70. _startTimer: function(){
  71. var self = this;
  72. this.timer = win.setTimeout(function(){
  73. self._keepAlive();
  74. }, this.options.pollingInterval * 1000);
  75. },
  76. _stopTimer: function(){
  77. // reset the failed requests counter
  78. this.failedRequests = this.options.failedRequests;
  79. win.clearTimeout(this.timer);
  80. },
  81. _keepAlive: function( recurse ){
  82. var self = this,
  83. options = this.options;
  84. //Reset the title to what it was.
  85. document.title = self.title;
  86. // assume a startTimer/keepAlive loop unless told otherwise
  87. if( typeof recurse === "undefined" ){
  88. recurse = true;
  89. }
  90. // if too many requests failed, abort
  91. if( !this.failedRequests ){
  92. this._stopTimer();
  93. options.onAbort.call( this.warning[0] );
  94. return;
  95. }
  96. $.ajax({
  97. timeout: options.AJAXTimeout,
  98. url: options.keepAliveURL,
  99. error: function(){
  100. self.failedRequests--;
  101. },
  102. success: function(response){
  103. if($.trim(response) !== options.serverResponseEquals){
  104. self.failedRequests--;
  105. }
  106. },
  107. complete: function(){
  108. if( recurse ){
  109. self._startTimer();
  110. }
  111. }
  112. });
  113. }
  114. };
  115. // expose
  116. $.idleTimeout = function(element, resume, options){
  117. idleTimeout.init( element, resume, $.extend($.idleTimeout.options, options) );
  118. return this;
  119. };
  120. // options
  121. $.idleTimeout.options = {
  122. // number of seconds after user is idle to show the warning
  123. warningLength: 30,
  124. // url to call to keep the session alive while the user is active
  125. keepAliveURL: "",
  126. // the response from keepAliveURL must equal this text:
  127. serverResponseEquals: "OK",
  128. // user is considered idle after this many seconds. 10 minutes default
  129. idleAfter: 600,
  130. // a polling request will be sent to the server every X seconds
  131. pollingInterval: 60,
  132. // number of failed polling requests until we abort this script
  133. failedRequests: 5,
  134. // the $.ajax timeout in MILLISECONDS!
  135. AJAXTimeout: 250,
  136. // %s will be replaced by the counter value
  137. titleMessage: 'Warning: %s seconds until log out | ',
  138. /*
  139. Callbacks
  140. "this" refers to the element found by the first selector passed to $.idleTimeout.
  141. */
  142. // callback to fire when the session times out
  143. onTimeout: $.noop,
  144. // fires when the user becomes idle
  145. onIdle: $.noop,
  146. // fires during each second of warningLength
  147. onCountdown: $.noop,
  148. // fires when the user resumes the session
  149. onResume: $.noop,
  150. // callback to fire when the script is aborted due to too many failed requests
  151. onAbort: $.noop
  152. };
  153. })(jQuery, window);