当前位置: 首页 > 文档资料 > Tabris 中文文档 >

Timer(计时器)

优质
小牛编辑
114浏览
2023-12-01

该API可以在全局命名空间中使用。不需要明确地导入。

方法

clearInterval(id)

参数:

  • id: any
    • setInterval返回的ID。

取消与给定ID关联的定时器的运行。当给定无效的ID时,什么都不会发生。

clearTimeout(id)

参数:

  • id: any
    • setTimeout返回的ID。

取消与给定ID关联的定时器的运行。当给定无效的ID时,什么都不会发生。

setInterval(callback, delay, …params)

参数:

  • callback: Function
    • 回调函数。
  • delay: number [可选]
    • 以毫秒为单位的延时。
  • …params: any[]
    • 传递给回调函数的一个或多个值。

返回值: any

重复调用给定的函数,每次调用都等待给定的延时时间。实际的延时可能比给定的时间稍长。

setTimeout(callback, delay, …params)

参数:

  • callback: Function
    • 回调函数。
  • delay: number [可选]
    • 以毫秒为单位的延时。
  • …params: any[]
    • 传递给回调函数的一个或多个值。

返回值: any

在指定的延时后使用param(以及后续的所有参数)来调用给定的函数。实际的延时可能比给定的时间稍长。

示例

const {Button, ui} = require('tabris');

new Button({
  centerX: 0, centerY: 0,
  text: 'Press me!'
}).on('select', ({target}) => {
  target.text = 'Please wait...';
  setTimeout(sayThanks, 2000, target);
}).appendTo(ui.contentView);

function sayThanks(widget) {
  widget.text = 'Thank you!';
}