123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**任务*/
- class TimeTask
- {
- public tid:number;
- public destTime:number;//延时执行时间
- public callback:Function;//执行方法
- public count:number;//执行次数
- public delay:number;//间隔
- public doDelete:boolean=false;
- constructor( tid:number,callback:Function, destTime:number,delay:number,count:number)
- {
- this.tid = tid;
- this.callback = callback;
- this.destTime = destTime;
- this.delay = delay;
- this.count = count;
- }
- }
- /**时间单位*/
- export enum TimeUnit
- {
- Millisecond,
- Second,
- Hour,
- Day
- }
- /**
- * 时间定时任务
- * 更好管理某一模块层级的时间任务,暂停,取消等
- */
- class TimeJobCenter {
- private tid:number = 0;
- private tempList:Array<TimeTask> = new Array<TimeTask>();
- private taskList:Array<TimeTask> = new Array<TimeTask>();
- private nowTime:number=0;
- public Dispose():void
- {
- this.taskList.length=0;
- this.tempList.length=0;
- this.nowTime=0;
- }
- private getTid():number
- {
- //安全代码以防过了
- if (this.tid==Number.MAX_VALUE)
- {
- let id = 0;
- while (true)
- {
- let used = false;
- for (let index = 0; index < this.taskList.length; index++)
- {
- if (this.taskList[index].tid == id)
- {
- used = true;
- break;
- }
- }
- if (!used)
- {
- break;
- }
- else
- {
- id++;
- }
- }
- return id;
- }else{
- this.tid += 1;
- return this.tid;
- }
- }
- /**延迟(ms)*/
- public delay(ms: number): Promise<void> {
- return new Promise(resolve => this.AddTimeTask(resolve, ms,1,TimeUnit.Millisecond));
- }
- /**延迟(s)*/
- public delay_second(ms: number): Promise<void> {
- return new Promise(resolve => this.AddTimeTask(resolve, ms,1,TimeUnit.Second));
- }
- /**
- * 增加时间任务
- * @param callback 执行方法
- * @param delay 延迟时间
- * @param count 次数
- * @param timeUnit 时间单位 默认为 秒 TimeUnit.Second
- * @returns 时间任务id
- */
- public AddTimeTask(callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):number
- {
- let tid = this.getTid();
- this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit));//增加一个任务到缓存
- return tid;
- }
- //
- private CreatNewTimeTask(tid:number, callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):TimeTask
- {
- if (timeUnit != TimeUnit.Second)
- {
- //如果单位不是秒,就全换算成秒做为单位
- switch (timeUnit)
- {
- case TimeUnit.Millisecond:
- delay *=0.001;
- break;
- case TimeUnit.Hour:
- delay *= 360;
- break;
- case TimeUnit.Day:
- delay *= 360 * 24;
- break;
- default:
- console.error("Add Task TimeUnit Type Error...");
- break;
- }
- }
- return new TimeTask(tid, callback,this.nowTime + delay, delay, count);
- }
- /**
- * 删除一个时间任务
- * @param tid 时间任务id
- * @returns 是否删除成功
- */
- public DeleteTimeTask(tid:number):boolean
- {
- let exist = false;
- let tt: TimeTask;
- for (let i = 0; i < this.tempList.length; i++)
- {
- tt = this.tempList[i];
- if (tt.tid == tid)
- {
- this.tempList[i].doDelete=true;
- exist = true;
- break;
- }
- }
- if (!exist)
- {
- for (let i = 0; i < this.taskList.length; i++)
- {
- tt = this.taskList[i];
- if (tt.tid == tid)
- {
- this.taskList[i].doDelete=true;
- exist = true;
- break;
- }
- }
- }
- return exist;
- }
- //
- private _index:number;
- private _count:number;
- private _tt:TimeTask;
- //运行调用
- public Run(dt:number):void
- {
- this.nowTime +=dt;
- this._count=this.tempList.length;
- if (this._count> 0)
- {
- for (this._index = 0;this._index < this._count; this._index++)
- {
- if(this.tempList[this._index].doDelete)continue;
- this.taskList.push(this.tempList[this._index]);
- }
- this.tempList.length=0;
- }
- this._count=this.taskList.length;
- if(this._count>0){
- for (this._index = 0; this._index <this._count; this._index++)
- {
- this._tt = this.taskList[this._index];
- if (this.nowTime < this._tt.destTime) continue;
- if (this._tt.doDelete) continue;
- this._tt.callback();
- if (this._tt.count == 1)
- {
- this._tt.doDelete=true;
- }
- else if (this._tt.count > 0)
- {
- this._tt.count--;
- this._tt.destTime += this._tt.delay;//下次执行时间
- }else{
- this._tt.destTime += this._tt.delay;
- }
- }
- }
- this.taskList = this.taskList.filter(task => !task.doDelete);//把标记为删除的真正删除
- }
- //
- }
- export default function get_new_job():TimeJobCenter { return new TimeJobCenter();}
|