jquery.idletimeout.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if(options.keepAliveURL != null && options.keepAliveURL.length > 0) {
  97. $.ajax({
  98. timeout: options.AJAXTimeout,
  99. url: options.keepAliveURL,
  100. error: function() {
  101. self.failedRequests--;
  102. },
  103. success: function(response) {
  104. if($.trim(response) !== options.serverResponseEquals) {
  105. self.failedRequests--;
  106. }
  107. },
  108. complete: function() {
  109. if(recurse){
  110. self._startTimer();
  111. }
  112. }
  113. });
  114. } else {
  115. if (recurse) {
  116. self._startTimer();
  117. }
  118. }
  119. }
  120. };
  121. // expose
  122. $.idleTimeout = function(element, resume, options){
  123. idleTimeout.init( element, resume, $.extend($.idleTimeout.options, options) );
  124. return this;
  125. };
  126. // options
  127. $.idleTimeout.options = {
  128. // number of seconds after user is idle to show the warning
  129. warningLength: 30,
  130. // url to call to keep the session alive while the user is active
  131. keepAliveURL: "",
  132. // the response from keepAliveURL must equal this text:
  133. serverResponseEquals: "OK",
  134. // user is considered idle after this many seconds. 10 minutes default
  135. idleAfter: 600,
  136. // a polling request will be sent to the server every X seconds
  137. pollingInterval: 60,
  138. // number of failed polling requests until we abort this script
  139. failedRequests: 5,
  140. // the $.ajax timeout in MILLISECONDS!
  141. AJAXTimeout: 250,
  142. // %s will be replaced by the counter value
  143. titleMessage: "Warning: %s seconds until log out | ",
  144. /*
  145. Callbacks
  146. "this" refers to the element found by the first selector passed to $.idleTimeout.
  147. */
  148. // callback to fire when the session times out
  149. onTimeout: $.noop,
  150. // fires when the user becomes idle
  151. onIdle: $.noop,
  152. // fires during each second of warningLength
  153. onCountdown: $.noop,
  154. // fires when the user resumes the session
  155. onResume: $.noop,
  156. // callback to fire when the script is aborted due to too many failed requests
  157. onAbort: $.noop
  158. };
  159. })(jQuery, window);