Greasy Fork is available in English.

jQuery.timers

Timer abstractions for jQuery

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/3465/10415/jQuerytimers.js

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