You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.4 KiB
95 lines
2.4 KiB
/*
|
|
* @Descripttion:
|
|
* @version: 1.0.0
|
|
* @Author: YeeChan
|
|
* @Date: 2020-07-23 16:59:58
|
|
*/
|
|
/**
|
|
* Timer工具
|
|
*/
|
|
export class TimerUtils {
|
|
|
|
/**
|
|
* 循环执行 有限次数
|
|
* @param handler 回调
|
|
* @param interval 间隔时间 秒
|
|
* @param repeat 重复次数(实际运行repeat + 1次)
|
|
* @param delay 延迟多少时间后开始执行 秒
|
|
*/
|
|
public static loopNum_custom(handler: Function, interval: number, repeat: number, delay: number) {
|
|
return this.schedule_custom(handler, interval, repeat, delay);
|
|
}
|
|
|
|
|
|
/**
|
|
* 延迟执行一次。
|
|
* @param handler 回调
|
|
* @param delayTime 延迟时间 秒
|
|
*/
|
|
public static once_custom(handler: Function, delayTime: number = 0) {
|
|
return this.scheduleOnce_custom(handler, delayTime);
|
|
}
|
|
|
|
|
|
/**
|
|
* 循环执行 一直循环
|
|
* @param handler 回调
|
|
* @param intervlTime 间隔时间 秒 值为 0,那么回调函数每一帧都会被调用
|
|
* @param delay 延迟时间执行 秒
|
|
*/
|
|
public static loop_custom(handler: Function, intervlTime: number = 0, delay: number = 0) {
|
|
return this.schedule_custom(handler, intervlTime ? intervlTime : 0.02, cc.macro.REPEAT_FOREVER, delay);
|
|
}
|
|
|
|
|
|
/**
|
|
* 移除计时器Timer
|
|
* @param handler 回调
|
|
*/
|
|
public static removeTimer_custom(handler: Function) {
|
|
this.unschedule_custom(handler);
|
|
}
|
|
|
|
|
|
/**
|
|
* 移除所有Timer
|
|
*/
|
|
public static removeAllTimers_custom() {
|
|
cc.director.getScheduler().unscheduleAllForTarget(this)
|
|
}
|
|
|
|
|
|
/**
|
|
* 单次调度
|
|
* @param handler
|
|
* @param delay
|
|
*/
|
|
private static scheduleOnce_custom(handler: Function, delay: number) {
|
|
this.schedule_custom(handler, 0, 0, delay);
|
|
return handler;
|
|
}
|
|
|
|
|
|
/**
|
|
* Timer开始调度
|
|
* @param handler 回调
|
|
* @param interval 间隔时间
|
|
* @param repeat 重复次数(实际运行repeat + 1次)
|
|
* @param delay 延迟多少时间后开始执行
|
|
*/
|
|
private static schedule_custom(handler: Function, interval: number, repeat: number, delay: number) {
|
|
cc.director.getScheduler().schedule(handler, this, interval, repeat, delay);
|
|
return handler;
|
|
}
|
|
|
|
|
|
/**
|
|
* 移除调度
|
|
* @param handler
|
|
*/
|
|
private static unschedule_custom(handler: Function) {
|
|
if (!handler) return;
|
|
cc.director.getScheduler().unschedule(handler, this);
|
|
}
|
|
|
|
} |