jquery.timers.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. jQuery.fn.extend({
  2. everyTime: function(interval, label, fn, times, belay) {
  3. return this.each(function() {
  4. jQuery.timer.add(this, interval, label, fn, times, belay);
  5. });
  6. },
  7. oneTime: function(interval, label, fn) {
  8. return this.each(function() {
  9. jQuery.timer.add(this, interval, label, fn, 1);
  10. });
  11. },
  12. stopTime: function(label, fn) {
  13. return this.each(function() {
  14. jQuery.timer.remove(this, label, fn);
  15. });
  16. }
  17. });
  18. jQuery.extend({
  19. timer: {
  20. guid: 1,
  21. global: {},
  22. regex: /^([0-9]+)\s*(.*s)?$/,
  23. powers: {
  24. // Yeah this is major overkill...
  25. 'ms': 1,
  26. 'cs': 10,
  27. 'ds': 100,
  28. 's': 1000,
  29. 'das': 10000,
  30. 'hs': 100000,
  31. 'ks': 1000000
  32. },
  33. timeParse: function(value) {
  34. if (value == undefined || value == null)
  35. return null;
  36. var result = this.regex.exec(jQuery.trim(value.toString()));
  37. if (result[2]) {
  38. var num = parseInt(result[1], 10);
  39. var mult = this.powers[result[2]] || 1;
  40. return num * mult;
  41. } else {
  42. return value;
  43. }
  44. },
  45. add: function(element, interval, label, fn, times, belay) {
  46. var counter = 0;
  47. if (jQuery.isFunction(label)) {
  48. if (!times)
  49. times = fn;
  50. fn = label;
  51. label = interval;
  52. }
  53. interval = jQuery.timer.timeParse(interval);
  54. if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
  55. return;
  56. if (times && times.constructor != Number) {
  57. belay = !!times;
  58. times = 0;
  59. }
  60. times = times || 0;
  61. belay = belay || false;
  62. if (!element.$timers)
  63. element.$timers = {};
  64. if (!element.$timers[label])
  65. element.$timers[label] = {};
  66. fn.$timerID = fn.$timerID || this.guid++;
  67. var handler = function() {
  68. if (belay && this.inProgress)
  69. return;
  70. this.inProgress = true;
  71. if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
  72. jQuery.timer.remove(element, label, fn);
  73. this.inProgress = false;
  74. };
  75. handler.$timerID = fn.$timerID;
  76. if (!element.$timers[label][fn.$timerID])
  77. element.$timers[label][fn.$timerID] = window.setInterval(handler, interval);
  78. if (!this.global[label])
  79. this.global[label] = [];
  80. this.global[label].push(element);
  81. },
  82. remove: function(element, label, fn) {
  83. var timers = element.$timers, ret;
  84. if (timers) {
  85. if (!label) {
  86. for (label in timers)
  87. this.remove(element, label, fn);
  88. } else if (timers[label]) {
  89. if (fn) {
  90. if (fn.$timerID) {
  91. window.clearInterval(timers[label][fn.$timerID]);
  92. delete timers[label][fn.$timerID];
  93. }
  94. } else {
  95. for (var fn in timers[label]) {
  96. window.clearInterval(timers[label][fn]);
  97. delete timers[label][fn];
  98. }
  99. }
  100. for (ret in timers[label]) break;
  101. if (!ret) {
  102. ret = null;
  103. delete timers[label];
  104. }
  105. }
  106. for (ret in timers) break;
  107. if (!ret)
  108. element.$timers = null;
  109. }
  110. }
  111. }
  112. });
  113. if (jQuery.browser.msie)
  114. jQuery(window).one("unload", function() {
  115. var global = jQuery.timer.global;
  116. for ( var label in global ) {
  117. var els = global[label], i = els.length;
  118. while ( --i )
  119. jQuery.timer.remove(els[i], label);
  120. }
  121. });