jquery.periodic.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*!
  2. * jQuery periodic plugin
  3. *
  4. * Copyright 2010, Tom Anderson
  5. * Dual licensed under the MIT or GPL Version 2 licenses.
  6. *
  7. */
  8. jQuery.periodic = function (options, callback) {
  9. // if the first argument is a function then assume the options aren't being passed
  10. if (jQuery.isFunction(options)) {
  11. callback = options;
  12. options = {};
  13. }
  14. // Merge passed settings with default values
  15. var settings = jQuery.extend({}, jQuery.periodic.defaults, {
  16. ajax_complete : ajaxComplete,
  17. increment : increment,
  18. reset : reset,
  19. cancel : cancel
  20. }, options);
  21. // bookkeeping variables
  22. settings.cur_period = settings.period;
  23. settings.tid = false;
  24. var prev_ajax_response = '';
  25. run();
  26. // return settings so user can tweak them externally
  27. return settings;
  28. // run (or restart if already running) the looping construct
  29. function run() {
  30. // clear/stop existing timer (multiple calls to run() won't result in multiple timers)
  31. cancel();
  32. // let it rip!
  33. settings.tid = setTimeout(function() {
  34. // set the context (this) for the callback to the settings object
  35. callback.call(settings);
  36. // compute the next value for cur_period
  37. increment();
  38. // queue up the next run
  39. if(settings.tid)
  40. run();
  41. }, settings.cur_period);
  42. }
  43. // utility function for use with ajax calls
  44. function ajaxComplete(xhr, status) {
  45. if (status === 'success' && prev_ajax_response !== xhr.responseText) {
  46. // reset the period whenever the response changes
  47. prev_ajax_response = xhr.responseText;
  48. reset();
  49. }
  50. }
  51. // compute the next delay
  52. function increment() {
  53. settings.cur_period *= settings.decay;
  54. if (settings.cur_period < settings.period) {
  55. // don't let it drop below the minimum
  56. reset();
  57. } else if (settings.cur_period > settings.max_period) {
  58. settings.cur_period = settings.max_period;
  59. if (settings.on_max !== undefined) {
  60. // call the user-supplied callback if we reach max_period
  61. settings.on_max.call(settings);
  62. }
  63. }
  64. }
  65. function reset() {
  66. settings.cur_period = settings.period;
  67. // restart with the new timeout
  68. run();
  69. }
  70. function cancel() {
  71. clearTimeout(settings.tid);
  72. settings.tid = null;
  73. }
  74. // other functions we might want to implement
  75. function pause() {}
  76. function resume() {}
  77. function log() {}
  78. };
  79. jQuery.periodic.defaults = {
  80. period : 4000, // 4 sec.
  81. max_period : 1800000, // 30 min.
  82. decay : 1.5, // time period multiplier
  83. on_max : undefined // called if max_period is reached
  84. };
  85. /*!
  86. * jQuery periodic plugin
  87. *
  88. * Copyright 2010, Tom Anderson
  89. * Dual licensed under the MIT or GPL Version 2 licenses.
  90. *
  91. */
  92. jQuery.periodic = function (options, callback) {
  93. // if the first argument is a function then assume the options aren't being passed
  94. if (jQuery.isFunction(options)) {
  95. callback = options;
  96. options = {};
  97. }
  98. // Merge passed settings with default values
  99. var settings = jQuery.extend({}, jQuery.periodic.defaults, {
  100. ajax_complete : ajaxComplete,
  101. increment : increment,
  102. reset : reset,
  103. cancel : cancel
  104. }, options);
  105. // bookkeeping variables
  106. settings.cur_period = settings.period;
  107. settings.tid = false;
  108. var prev_ajax_response = '';
  109. run();
  110. // return settings so user can tweak them externally
  111. return settings;
  112. // run (or restart if already running) the looping construct
  113. function run() {
  114. // clear/stop existing timer (multiple calls to run() won't result in multiple timers)
  115. cancel();
  116. // let it rip!
  117. settings.tid = setTimeout(function() {
  118. // set the context (this) for the callback to the settings object
  119. callback.call(settings);
  120. // compute the next value for cur_period
  121. increment();
  122. // queue up the next run
  123. if(settings.tid)
  124. run();
  125. }, settings.cur_period);
  126. }
  127. // utility function for use with ajax calls
  128. function ajaxComplete(xhr, status) {
  129. if (status === 'success' && prev_ajax_response !== xhr.responseText) {
  130. // reset the period whenever the response changes
  131. prev_ajax_response = xhr.responseText;
  132. reset();
  133. }
  134. }
  135. // compute the next delay
  136. function increment() {
  137. settings.cur_period *= settings.decay;
  138. if (settings.cur_period < settings.period) {
  139. // don't let it drop below the minimum
  140. reset();
  141. } else if (settings.cur_period > settings.max_period) {
  142. settings.cur_period = settings.max_period;
  143. if (settings.on_max !== undefined) {
  144. // call the user-supplied callback if we reach max_period
  145. settings.on_max.call(settings);
  146. }
  147. }
  148. }
  149. function reset() {
  150. settings.cur_period = settings.period;
  151. // restart with the new timeout
  152. run();
  153. }
  154. function cancel() {
  155. clearTimeout(settings.tid);
  156. settings.tid = null;
  157. }
  158. // other functions we might want to implement
  159. function pause() {}
  160. function resume() {}
  161. function log() {}
  162. };
  163. jQuery.periodic.defaults = {
  164. period : 4000, // 4 sec.
  165. max_period : 1800000, // 30 min.
  166. decay : 1.5, // time period multiplier
  167. on_max : undefined // called if max_period is reached
  168. };