TimeJobCenter.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**任务*/
  2. class TimeTask
  3. {
  4. public tid:number;
  5. public destTime:number;//延时执行时间
  6. public callback:Function;//执行方法
  7. public count:number;//执行次数
  8. public delay:number;//间隔
  9. public doDelete:boolean=false;
  10. constructor( tid:number,callback:Function, destTime:number,delay:number,count:number)
  11. {
  12. this.tid = tid;
  13. this.callback = callback;
  14. this.destTime = destTime;
  15. this.delay = delay;
  16. this.count = count;
  17. }
  18. }
  19. /**时间单位*/
  20. export enum TimeUnit
  21. {
  22. Millisecond,
  23. Second,
  24. Hour,
  25. Day
  26. }
  27. /**
  28. * 时间定时任务
  29. * 更好管理某一模块层级的时间任务,暂停,取消等
  30. */
  31. export class TimeJobCenter {
  32. private tid:number = 0;
  33. private tempList:Array<TimeTask> = new Array<TimeTask>();
  34. private taskList:Array<TimeTask> = new Array<TimeTask>();
  35. private nowTime:number=0;
  36. public Dispose():void
  37. {
  38. this.taskList.length=0;
  39. this.tempList.length=0;
  40. this.nowTime=0;
  41. }
  42. private getTid():number
  43. {
  44. //安全代码以防过了
  45. if (this.tid==Number.MAX_VALUE)
  46. {
  47. let id = 0;
  48. while (true)
  49. {
  50. let used = false;
  51. for (let index = 0; index < this.taskList.length; index++)
  52. {
  53. if (this.taskList[index].tid == id)
  54. {
  55. used = true;
  56. break;
  57. }
  58. }
  59. if (!used)
  60. {
  61. break;
  62. }
  63. else
  64. {
  65. id++;
  66. }
  67. }
  68. return id;
  69. }else{
  70. this.tid += 1;
  71. return this.tid;
  72. }
  73. }
  74. /**
  75. * 增加时间任务
  76. * @param callback 执行方法
  77. * @param delay 延迟时间
  78. * @param count 次数
  79. * @param timeUnit 时间单位 默认为 秒 TimeUnit.Second
  80. * @returns 时间任务id
  81. */
  82. public AddTimeTask(callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):number
  83. {
  84. let tid = this.getTid();
  85. this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit));//增加一个任务到缓存
  86. return tid;
  87. }
  88. //
  89. private CreatNewTimeTask(tid:number, callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):TimeTask
  90. {
  91. if (timeUnit != TimeUnit.Second)
  92. {
  93. //如果单位不是秒,就全换算成秒做为单位
  94. switch (timeUnit)
  95. {
  96. case TimeUnit.Millisecond:
  97. delay *=0.001;
  98. break;
  99. case TimeUnit.Hour:
  100. delay *= 360;
  101. break;
  102. case TimeUnit.Day:
  103. delay *= 360 * 24;
  104. break;
  105. default:
  106. console.error("Add Task TimeUnit Type Error...");
  107. break;
  108. }
  109. }
  110. return new TimeTask(tid, callback,this.nowTime + delay, delay, count);
  111. }
  112. /**
  113. * 删除一个时间任务
  114. * @param tid 时间任务id
  115. * @returns 是否删除成功
  116. */
  117. public DeleteTimeTask(tid:number):boolean
  118. {
  119. let exist = false;
  120. let tt: TimeTask;
  121. for (let i = 0; i < this.tempList.length; i++)
  122. {
  123. tt = this.tempList[i];
  124. if (tt.tid == tid)
  125. {
  126. this.tempList[i].doDelete=true;
  127. exist = true;
  128. break;
  129. }
  130. }
  131. if (!exist)
  132. {
  133. for (let i = 0; i < this.taskList.length; i++)
  134. {
  135. tt = this.taskList[i];
  136. if (tt.tid == tid)
  137. {
  138. this.taskList[i].doDelete=true;
  139. exist = true;
  140. break;
  141. }
  142. }
  143. }
  144. return exist;
  145. }
  146. //
  147. private _index:number;
  148. private _count:number;
  149. private _tt:TimeTask;
  150. //运行调用
  151. public Run(dt:number):void
  152. {
  153. this.nowTime +=dt;
  154. this._count=this.tempList.length;
  155. if (this._count> 0)
  156. {
  157. for (this._index = 0;this._index < this._count; this._index++)
  158. {
  159. if(this.tempList[this._index].doDelete)continue;
  160. this.taskList.push(this.tempList[this._index]);
  161. }
  162. this.tempList.length=0;
  163. }
  164. this._count=this.taskList.length;
  165. if(this._count>0){
  166. for (this._index = 0; this._index <this._count; this._index++)
  167. {
  168. this._tt = this.taskList[this._index];
  169. if (this.nowTime < this._tt.destTime) continue;
  170. if (this._tt.doDelete) continue;
  171. this._tt.callback();
  172. if (this._tt.count == 1)
  173. {
  174. this._tt.doDelete=true;
  175. }
  176. else if (this._tt.count > 0)
  177. {
  178. this._tt.count--;
  179. this._tt.destTime += this._tt.delay;//下次执行时间
  180. }else{
  181. this._tt.destTime += this._tt.delay;
  182. }
  183. }
  184. }
  185. this.taskList = this.taskList.filter(task => !task.doDelete);//把标记为删除的真正删除
  186. }
  187. //
  188. }