learTimeout(0) 会清理掉setInterval 中 timerId = 0 的任务

clearTimeout(0) 会清理掉setInterval 中 timerId = 0 的任务。

const timerId = setInterval(() => { 
  let testTag = 'dd'; 
  hilog.info(0x0000, 'testTag', '%{public}s', 'setInterval'); 
}, 300) 
hilog.info(0x0000, 'testTag', '%{public}s', 'timerId = ' + timerId); 
// clearInterval(0) 
clearTimeout(0)
HarmonyOS
2024-06-04 23:53:34
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
daxiake1

clearTimeout与clearInterval相互清理为原生js的问题,建议针对setInterval与setTimeout的返回值使用不同变量存值,清理时清理对应变量,setInterval与setTimeout的返回值会相互累加。

示例代码:

import hilog from '@ohos.hilog'; 
 
const TAG: string = 'testTag' 
 
@Entry 
@Component 
struct TimeoutPage { 
  private timeoutId: number = -1; 
  private intervalId: number = -1; 
 
  aboutToAppear() { 
    this.timeoutId = setTimeout(() => { 
      hilog.info(0x00000, TAG, 'setTimeout') 
    }, 0) 
 
    this.intervalId = setInterval(() => { 
      hilog.info(0x00000, TAG, 'setInterval') 
    }, 1000) 
 
    hilog.info(0x00000, TAG, 'timeoutId:::' + this.timeoutId); 
    hilog.info(0x00000, TAG, 'intervalId:::' + this.intervalId); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text('清理定时器-intervalId') 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            clearInterval(this.intervalId); 
          }) 
          .margin({ 
            bottom: '3%' 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-06-05 20:12:42
相关问题
color使用color: 'rgba(0, 0, 255, .5)'不生效
791浏览 • 1回复 待解决
Failed to initialize CoreCLR, HRESULT: 0x80004005
1260浏览 • 1回复 待解决
鸿蒙AbilitySlice怎么finish
7613浏览 • 2回复 待解决
延迟任务什么时候执行
517浏览 • 1回复 待解决
鸿蒙ScrollView如何禁滚动事件
5879浏览 • 2回复 待解决