harmony 鸿蒙Timer

2022-08-09 浏览 (894)

Timer

The Timer module provides basic timer capabilities. You can use the APIs of this module to execute functions at the specified time.

NOTE

The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

setTimeout

setTimeout(handler: Function|string, delay?: number, ...arguments: any[]): number

Sets a timer for the system to call a function after the timer goes off.

The timer is automatically deleted after the callback is executed, and can be manually deleted by calling the clearTimeout API.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
handlerFunction |stringYesFunction to be called after the timer goes off. If the type is string, error information is printed and no other processing is performed.
delaynumberNoNumber of milliseconds delayed before the execution. If this parameter is left empty, the default value 0 is used, which means that the execution starts immediately or as soon as possible.
...argumentsany[]NoAdditional parameters to pass to the handler after the timer goes off.

Return value

TypeDescription
numberID of the timer. The value is an integer.

Example

setTimeout(() => {
  console.log('delay 1s');
}, 1000);

clearTimeout

clearTimeout(timeoutID?: number): void

Cancels the repeating timer set via setTimeout().

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
timeoutIDnumberNoID of the timer to cancel, which is returned by setTimeout() If this parameter is omitted, no timer is canceled.

Example

let timeoutID = setTimeout(() => {
  console.log('do after 1s delay.');
}, 1000);
clearTimeout(timeoutID);

setInterval

setInterval(handler: Function|string, delay: number, ...arguments: any[]): number

Sets a repeating timer for the system to repeatedly call a function at a fixed interval. The timer can only be manually deleted by calling the clearInterval API.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
handlerFunction |stringYesFunction to be called repeatedly. If the type is string, error information is printed and no other processing is performed.
delaynumberYesNumber of milliseconds delayed before the execution.
...argumentsany[]NoAdditional parameters to pass to the handler after the timer goes off.

Return value

TypeDescription
numberID of the timer. The value is an integer.

Example

setInterval(() => {
  console.log('do every 1s.');
}, 1000);

clearInterval

clearInterval(intervalID?: number): void

Cancels the repeating timer set via setInterval().

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
intervalIDnumberNoID of the repeating timer to cancel, which is returned by setInterval(). If this parameter is omitted, no timer is canceled.

Example

let intervalID = setInterval(() => {
  console.log('do every 1s.');
}, 1000);
clearInterval(intervalID);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7a6f445aa51a44ebb9195444719a225b