jquery.timers.js 3.1 KB

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