欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用 uniApp 设置和清除计时器

最编程 2024-04-26 06:58:28
...

首先是在data中定义一个变量,用来存放定时器

data() {
		return {
			timer: null,
        }
}

在适当的地方创建定时器

this.timer = setInterval(() => {
  console.log('111');
}, 10000)

在onHide或者是onUnload中销毁定时器,一般来说tabbar页面的切换会触发onHide,其他是onUnload,当然这也不是一定的,不确定的话可以先在这两个生命周期中console.log

if(this.timer) {  
   clearTimeout(this.timer); 
   this.timer = null;  
} 

推荐阅读