Digital Office Automation System Backend
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

jquery.asyncqueue.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * This file is part of the jquery plugin "asyncQueue".
  3. *
  4. * (c) Sebastien Roch <roch.sebastien@gmail.com>
  5. * @author (parallel) Dmitry Farafonov
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. (function($){
  11. $.AsyncQueue = function() {
  12. var that = this,
  13. queue = [],
  14. completeFunc,
  15. failureFunc,
  16. paused = false,
  17. lastCallbackData,
  18. _run,
  19. _complete,
  20. inQueue = 0,
  21. defaultTimeOut = 10;
  22. _run = function() {
  23. var f = queue.shift();
  24. if (f) {
  25. inQueue++;
  26. setTimeout(function(){
  27. f.fn.apply(that, [that]);
  28. if (!f.isParallel)
  29. if (paused === false) {
  30. _run();
  31. }
  32. inQueue --;
  33. if (inQueue == 0 && queue.length == 0)
  34. _complete();
  35. }, f.timeOut);
  36. if (f.isParallel)
  37. if (paused === false) {
  38. _run();
  39. }
  40. }
  41. };
  42. _complete = function(){
  43. if (completeFunc)
  44. completeFunc.apply(that, [that]);
  45. };
  46. this.onComplete = function(func) {
  47. completeFunc = func;
  48. };
  49. this.onFailure = function(func) {
  50. failureFunc = func;
  51. };
  52. this.add = function(func) {
  53. // TODO: add callback for queue[i] complete
  54. var obj = arguments[0];
  55. if (obj && Object.prototype.toString.call(obj) === "[object Array]") {
  56. var fn = arguments[1];
  57. var timeOut = (typeof(arguments[2]) != "undefined")? arguments[2] : defaultTimeOut;
  58. if (typeof(fn) == "function") {
  59. for(var i = 0; i < obj.length; i++) {
  60. var f = function(objx){
  61. queue.push({isParallel: true, fn: function(){fn.apply(that, [that, objx]);}, timeOut: timeOut});
  62. }(obj[i])
  63. }
  64. }
  65. } else {
  66. var fn = arguments[0];
  67. var timeOut = (typeof(arguments[1]) != "undefined")? arguments[2] : defaultTimeOut;
  68. queue.push({isParallel: false, fn: func, timeOut: timeOut});
  69. }
  70. return this;
  71. };
  72. this.addParallel = function(func, timeOut) {
  73. // TODO: add callback for queue[i] complete
  74. queue.push({isParallel: true, fn: func, timeOut: timeOut});
  75. return this;
  76. };
  77. this.storeData = function(dataObject) {
  78. lastCallbackData = dataObject;
  79. return this;
  80. };
  81. this.lastCallbackData = function () {
  82. return lastCallbackData;
  83. };
  84. this.run = function() {
  85. paused = false;
  86. _run();
  87. };
  88. this.pause = function () {
  89. paused = true;
  90. return this;
  91. };
  92. this.failure = function() {
  93. paused = true;
  94. if (failureFunc) {
  95. var args = [that];
  96. for(i = 0; i < arguments.length; i++) {
  97. args.push(arguments[i]);
  98. }
  99. failureFunc.apply(that, args);
  100. }
  101. };
  102. this.size = function(){
  103. return queue.length;
  104. };
  105. return this;
  106. }
  107. })(jQuery);