diff --git a/zfoo/scheduler/SchedulerBus.gd b/zfoo/scheduler/SchedulerBus.gd index f49cbdf..d57921c 100644 --- a/zfoo/scheduler/SchedulerBus.gd +++ b/zfoo/scheduler/SchedulerBus.gd @@ -9,12 +9,21 @@ const CollectionUtils = preload("res://zfoo/util/CollectionUtils.gd") const schedulerMap: Dictionary = {} + +# 固定延迟执行的任务,delay默认为毫秒 static func schedule(runnable: Runnable, delay: int) -> void: var triggerTimestamp = TimeUtils.currentTimeMillis() + delay var definition = SchedulerDefinition.new(runnable, delay, triggerTimestamp, false) schedulerMap[definition] = null +# 不断执行的周期循环任务,delay默认为毫秒 +static func scheduleAtFixRate(runnable: Runnable, delay: int) -> void: + var triggerTimestamp = TimeUtils.currentTimeMillis() + delay + var definition = SchedulerDefinition.new(runnable, delay, triggerTimestamp, false) + schedulerMap[definition] = null + + static func triggerPerSecond() -> void: var timestamp = TimeUtils.currentTimeMillis() diff --git a/zfoo/scheduler/TimeUtils.gd b/zfoo/scheduler/TimeUtils.gd index f729154..4c44788 100644 --- a/zfoo/scheduler/TimeUtils.gd +++ b/zfoo/scheduler/TimeUtils.gd @@ -1,5 +1,20 @@ extends Object + +# 一秒钟对应的纳秒数 +const NANO_PER_SECOND: int = 1_000_000_000 +# 一秒钟对应的毫秒数 +const MILLIS_PER_SECOND: int = 1 * 1000 +# 一分钟对应的毫秒数 +const MILLIS_PER_MINUTE = 1 * 60 * MILLIS_PER_SECOND +# 一个小时对应的毫秒数 +const MILLIS_PER_HOUR = 1 * 60 * MILLIS_PER_MINUTE +# 一天对应的毫秒数 +const MILLIS_PER_DAY = 1 * 24 * MILLIS_PER_HOUR +# 一周对应的毫秒数 +const MILLIS_PER_WEEK = 1 * 7 * MILLIS_PER_HOUR + + const timestamp = [0]