1e41f4b71Sopenharmony_ci# Timer (定时器) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供基础的定时器能力,支持按照指定的时间执行对应函数。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 开发者在UI界面中使用定时器时,定时器的触发机制会受UI底层原理管控,如果UI界面退到后台,定时器会被冻结。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## setTimeout 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_cisetTimeout(handler: Function | string, delay?: number, ...arguments: any[]): number 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci设置一个定时器,该定时器在定时器到期后执行一个函数。 16e41f4b71Sopenharmony_ci该定时器在回调被执行后自动删除,或使用clearTimeout接口手动删除。 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**参数:** 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 25e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 26e41f4b71Sopenharmony_ci| handler | Function \| string | 是 | 定时器到期后执行函数。类型为string则打印Error信息,不进行其他处理。 | 27e41f4b71Sopenharmony_ci| delay | number | 否 | 延迟的毫秒数,函数的调用会在该延迟之后发生。建议整数,若传入小数,会被向下取整。<br>如果省略该参数,delay取默认值0,意味着“马上”执行,更准确的说,在下一个事件循环执行。<br>注意:<br>1. 无论是哪种情况,实际延迟可能会比预期长一些。<br>2. 如果值小于1,会被默认取0。 | 28e41f4b71Sopenharmony_ci| ...arguments | any[] | 否 | 附加参数,一旦定时器到期,它们会作为参数传递给handler。 | 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**返回值:** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci| 类型 | 说明 | 33e41f4b71Sopenharmony_ci| -------- | -------- | 34e41f4b71Sopenharmony_ci| number | 该定时器的ID,定时器ID为进程共享,是从0开始顺序增加的整数,无重复值。 | 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**示例:** 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci ```ts 39e41f4b71Sopenharmony_ci setTimeout(() => { 40e41f4b71Sopenharmony_ci console.log('delay 1s'); 41e41f4b71Sopenharmony_ci }, 1000); 42e41f4b71Sopenharmony_ci ``` 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## clearTimeout 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciclearTimeout(timeoutID?: number): void 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci可取消通过调用setTimeout()建立的定时器。 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci定时器对象保存在创建它的线程内,删除定时器需要在创建该定时器的线程删除。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**参数:** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 60e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 61e41f4b71Sopenharmony_ci| timeoutID | number | 否 | 要取消定时器的ID, 是由setTimeout()返回的。如果省略该参数,则不取消任何定时任务,无任何处理。| 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**示例:** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```js 66e41f4b71Sopenharmony_ci let timeoutID = setTimeout(() => { 67e41f4b71Sopenharmony_ci console.log('do after 1s delay.'); 68e41f4b71Sopenharmony_ci }, 1000); 69e41f4b71Sopenharmony_ci clearTimeout(timeoutID); 70e41f4b71Sopenharmony_ci ``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci## setInterval 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_cisetInterval(handler: Function | string, delay: number, ...arguments: any[]): number 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci重复调用一个函数,在每次调用之间具有固定的时间延迟。 78e41f4b71Sopenharmony_ci删除该定时器需手动调用clearInterval接口。 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**参数:** 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 87e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 88e41f4b71Sopenharmony_ci| handler | Function \| string | 是 | 要重复调用的函数。类型为string则打印Error信息,不进行其他处理。| 89e41f4b71Sopenharmony_ci| delay | number | 是 | 延迟的毫秒数,函数的调用会在该延迟之后发生。 | 90e41f4b71Sopenharmony_ci| ...arguments | any[] | 否 | 附加参数,一旦定时器到期,他们会作为参数传递给handler。 | 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**返回值:** 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci| 类型 | 说明 | 95e41f4b71Sopenharmony_ci| -------- | -------- | 96e41f4b71Sopenharmony_ci| number | 该定时器的ID,定时器ID为进程共享,是从0开始顺序增加的整数,无重复值。 | 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**示例:** 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci ```js 101e41f4b71Sopenharmony_ci setInterval(() => { 102e41f4b71Sopenharmony_ci console.log('do every 1s.'); 103e41f4b71Sopenharmony_ci }, 1000); 104e41f4b71Sopenharmony_ci ``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci## clearInterval 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciclearInterval(intervalID?: number): void 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci可取消通过setInterval()设置的重复定时任务。 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci定时器对象保存在创建它的线程内,删除定时器需要在创建该定时器的线程删除。 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**参数:** 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 122e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 123e41f4b71Sopenharmony_ci| intervalID | number | 否 | 要取消的重复定时器的ID,是由 setInterval() 返回的。如果省略该参数,则不取消任何定时任务,无任何处理。| 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**示例:** 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci ```js 128e41f4b71Sopenharmony_ci let intervalID = setInterval(() => { 129e41f4b71Sopenharmony_ci console.log('do every 1s.'); 130e41f4b71Sopenharmony_ci }, 1000); 131e41f4b71Sopenharmony_ci clearInterval(intervalID); 132e41f4b71Sopenharmony_ci ``` 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci## 其他说明 135e41f4b71Sopenharmony_ci### 超时延迟 136e41f4b71Sopenharmony_ci如果页面正忙于其他任务,超时也可能比预期的晚。setTimeout的函数或代码片段是在下一个时间周期执行的。例如: 137e41f4b71Sopenharmony_ci ```ts 138e41f4b71Sopenharmony_ci function foo() { 139e41f4b71Sopenharmony_ci console.info('OH test foo is called') 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci setTimeout(foo, 0); 142e41f4b71Sopenharmony_ci console.info('After OH test setTimeout') 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci // output 145e41f4b71Sopenharmony_ci After OH test setTimeout 146e41f4b71Sopenharmony_ci OH test foo is called 147e41f4b71Sopenharmony_ci ``` 148e41f4b71Sopenharmony_ci这是因为,虽然setTimeout设置了0ms的延迟,但任务不是立即执行,而是会被放入队列中,等待下一次事件循环执行。当前正在执行的代码必须先完成,队列中的函数才会被执行,因此最终的执行顺序可能和预期不一致。 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci### 最大延迟值 151e41f4b71Sopenharmony_citimer内部以32位带符号整数存储延时,这就会导致如果一个延时大于2147483647毫秒(大约24.8天)时就会溢出,导致定时器将会被立即执行。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci### 定时器冻结 154e41f4b71Sopenharmony_ci定时器的触发受底层任务调度。当前应用被切换到后台后,定时器到期也不会触发。应用被重新拉起到前台后,到期定时器会按序触发。可使用trace查看进程是否还存在调度,如果没有调度,则定时器被冻结。 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci### 定时器ID 157e41f4b71Sopenharmony_cisetTimeout()和setInterval()使用共享的ID池,意味着在技术上可以混用clearTimeout()和clearInterval()。但出于代码清晰性考虑,我们应该避免混用它们。