渐进式编程课
Cocos Creator 目录
第 15 课2026.09.21

避免 Inspector 中的零或负间隔进入 schedule。

第 15 课:限制调度间隔下限

  • 日期:2026-09-21
  • 课程序号:第 15 课
  • 知识点:限制调度间隔下限

用途或适用场景

避免 Inspector 中的零或负间隔进入 schedule。

核心概念

仍保留 @property 的 interval;调用 schedule 时使用正数下限,不复制调度状态。

最小代码或操作示例

text
@property
interval = 1
onEnable(): void {
  const seconds = Math.max(this.interval, 0.1)
  this.schedule(this.tick, seconds)
}

3~5 分钟练习

把 Inspector 中 interval 设为 0。

参考答案

实际调度间隔为 0.1 秒。

与上一课的联系

沿用第 14 课的 interval 属性、tick 回调和对称清理;本课只新增 Math.max 限制传给 schedule 的下限;下一课可给 Inspector 中的属性标明单位。

时效校验

时效校验:2026-09-28(已核对 Cocos Creator 3.8 Scheduler 手册及 3.8.9 Component 源码;移除组件不存在的 isScheduled 调用)。

官方参考