arkts 定时器的使用
·
@State counter: number = 0;
@State intervalId: number = 1;
timerFunction() {
// 清除之前的定时器(如果存在)
if (this.intervalId) {
clearInterval(this.intervalId);
}
// 设置一个定时器,在24小时内每10分钟调用一次接口
this.intervalId = setInterval(()=>{
this.counter++;
if ((this.counter % 3) == 1) {
this.inputType = InputType.GetReport;
} else if ((this.counter % 3) == 2) {
this.inputType = InputType.ReadBlock;
} else {
this.inputType = InputType.ReadNonBlock;
}
hilog.info(0, 'testTag ui', `[timerFunction] inputType: ${this.inputType}`);
doSomeThing(this.inputType);
hilog.info(0, 'testTag ui', `[timerFunction] inputType: ${this.inputType}`);
}, 10 * 60 * 1000);
// 设置一个额外的定时器来在24小时后清除这个定时器
setTimeout(() => {
clearInterval(this.intervalId);
console.log('24小时结束,停止调用接口 输入');
}, 24 * 60 * 60 * 1000);
}
为了测试App的稳定性,在界面上某个按钮的click事件中,起了个定时器,定时器每隔10分钟调用一次doSomeThing(inputType: number),另外再起一个超时定时器在24小时后超时结束。
如果程序中需要设置多个定时器时,不同的定时器需要设置不同的intervalId。
更多推荐

所有评论(0)