# TimerManager | oops-framework
Documentation for oops-framework

# Class TimerManager

时间管理

#### Hierarchy

- EventDispatcher

  #### Hierarchy

  - TimerManager

- Defined in assets/core/common/manager/TimerManager.ts:6

#####
Index

### Constructors

constructor

### Properties

_msg
component
initTime
scheduleCount
schedules
serverTime
times

### Methods

destroy
dispatchEvent
format
getLocalTime
getTime
load
off
on
onTimerComplete
onUpdate
register
save
schedule
scheduleOnce
setServerTime
unRegister
unschedule
unscheduleAll

## Constructors

### constructor

- new TimerManager(component: Component): TimerManager

- 构造函数

  #### Parameters

  - ##### component: Component

    cc.Component对象

  #### Returns TimerManager

  Overrides EventDispatcher.constructor

  - Defined in assets/core/common/manager/TimerManager.ts:21

## Properties

### `Protected` _msg

_msg: null | MessageEventData = null

Inherited from EventDispatcher._msg

- Defined in assets/core/common/event/EventDispatcher.ts:12

### `Private` component

component: Component

- Defined in assets/core/common/manager/TimerManager.ts:12

### `Private` initTime

initTime: number = ...

- Defined in assets/core/common/manager/TimerManager.ts:11

### `Private` scheduleCount

scheduleCount: number = 1

- Defined in assets/core/common/manager/TimerManager.ts:9

### `Private` schedules

schedules: any = {}

- Defined in assets/core/common/manager/TimerManager.ts:8

### `Private` serverTime

serverTime: number = 0

- Defined in assets/core/common/manager/TimerManager.ts:15

### `Static` `Private` times

times: any = {}

- Defined in assets/core/common/manager/TimerManager.ts:7

## Methods

### destroy

- destroy(): void

- 销毁事件对象

  #### Returns void

  Inherited from EventDispatcher.destroy

  - Defined in assets/core/common/event/EventDispatcher.ts:52

### dispatchEvent

- dispatchEvent(event: string, args?: any): void

- 触发全局事件

  #### Parameters

  - ##### event: string

    事件名

  - ##### args: any = null

    事件参数

  #### Returns void

  Inherited from EventDispatcher.dispatchEvent

  - Defined in assets/core/common/event/EventDispatcher.ts:42

### format

- format(format: string, date: Date): string

- 格式化日期显示

  #### Parameters

  - ##### format: string

    格式化字符串（例：yyyy-MM-dd hh:mm:ss）

  - ##### date: Date

    时间对象

  #### Returns string

  - Defined in assets/core/common/manager/TimerManager.ts:44

### getLocalTime

- getLocalTime(): number

- 获取本地时间刻度

  #### Returns number

  - Defined in assets/core/common/manager/TimerManager.ts:72

### getTime

- getTime(): number

- 获取游戏开始到现在逝去的时间

  #### Returns number

  - Defined in assets/core/common/manager/TimerManager.ts:67

### load

- load(): void

- 游戏最大化时回复时间数据

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:234

### off

- off(event: string): void

- 移除全局事件

  #### Parameters

  - ##### event: string

    事件名

  #### Returns void

  Inherited from EventDispatcher.off

  - Defined in assets/core/common/event/EventDispatcher.ts:31

### on

- on(event: string, listener: ListenerFunc, object: any): void

- 注册全局事件

  #### Parameters

  - ##### event: string

    事件名

  - ##### listener: ListenerFunc

    处理事件的侦听器函数

  - ##### object: any

    侦听函数绑定的作用域对象

  #### Returns void

  Inherited from EventDispatcher.on

  - Defined in assets/core/common/event/EventDispatcher.ts:20

### `Private` onTimerComplete

- onTimerComplete(data: any): void

- 触发倒计时完成事件

  #### Parameters

  - ##### data: any

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:163

### `Private` onUpdate

- onUpdate(dt: number): void

- #### Parameters

  - ##### dt: number

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:143

### register

- register(object: any, field: string, onSecond: Function, onComplete: Function): any

- 在指定对象上注册一个倒计时的回调管理器

  ### Returns

  ### Example

      export class Test extends Component {
         private timeId!: string;

         start() {
             // 在指定对象上注册一个倒计时的回调管理器
             this.timeId = oops.timer.register(this, "countDown", this.onSecond, this.onComplete);
         }

         private onSecond() {
             console.log("每秒触发一次");
         }

         private onComplete() {
             console.log("倒计时完成触发");
         }
      }

  #### Parameters

  - ##### object: any

    注册定时器的对象

  - ##### field: string

    时间字段

  - ##### onSecond: Function

    每秒事件

  - ##### onComplete: Function

    倒计时完成事件

  #### Returns any

  - Defined in assets/core/common/manager/TimerManager.ts:193

### save

- save(): void

- 游戏最小化时记录时间数据

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:227

### schedule

- schedule(callback: Function, interval: number): string

- 注册一个固定间隔时间的触发器

  ### Returns

  string

  ### Example

      oops.timer.schedule(()=>{
         // 每秒触发一次
      }, 1000);

  #### Parameters

  - ##### callback: Function

    触发时的回调方法

  - ##### interval: number

    固定间隔触发时间

  #### Returns string

  - Defined in assets/core/common/manager/TimerManager.ts:86

### scheduleOnce

- scheduleOnce(callback: Function, delay?: number): string

- 注册一个只触发一次的延时的触发器

  ### Returns

  string

  ### Example

      oops.timer.scheduleOnce(()=>{
         // 1秒后触发一次后不会在触发
      }, 1000);

  #### Parameters

  - ##### callback: Function

    触发时的回调方法

  - ##### delay: number = 0

    延时触发时间

  #### Returns string

  - Defined in assets/core/common/manager/TimerManager.ts:103

### setServerTime

- setServerTime(val?: number): number

- 服务器时间与本地时间同步

  #### Parameters

  - ##### `Optional` val: number

    服务器时间刻度

  #### Returns number

  - Defined in assets/core/common/manager/TimerManager.ts:32

### unRegister

- unRegister(id: string): void

- 在指定对象上注销一个倒计时的回调管理器

  ### Example

      export class Test extends Component {
         private timeId!: string;

         start() {
             this.timeId = oops.timer.register(this, "countDown", this.onSecond, this.onComplete);
         }

         onDestroy() {
             // 在指定对象上注销一个倒计时的回调管理器
             oops.timer.unRegister(this.timeId);
         }
      }

  #### Parameters

  - ##### id: string

    时间对象唯一表示

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:221

### unschedule

- unschedule(uuid: string): void

- 删除一个时间触发器

  ### Example

      var uuid = oops.timer.schedule(()=>{
         // 每秒触发一次
      }, 1000);

      // 删除指定标识的触发器
      oops.timer.unschedule(uuid);

  #### Parameters

  - ##### uuid: string

    唯一标识

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:127

### unscheduleAll

- unscheduleAll(): void

- 删除所有时间触发器

  #### Returns void

  - Defined in assets/core/common/manager/TimerManager.ts:136

Generated using TypeDoc