1. uniapp 使用定时器和清除定时器

1.1. 定义一个timer

data(){
    return{
        timer: null
    }
}

1.2. 设置定时器

//选择适合需求的定时器
this.timer = setTimeout( () => {
    // 这里添加您的逻辑		
}, 1000)
this.timer = setInterval( () => {
    // 同上			
}, 1000)

1.3. 清除定时器

  这里需要注意的是我们页面中使用了定时器,在离开这个页面的时候一定要记得清除,避免出现bug。

//一般页面用onUnload
onUnload() {
	if(this.timer) {  
		clearTimeout(this.timer);  
		this.timer = null;  
	}  
}
tabbar页面用onHide
onHide() {
	if(this.timer) {  
		clearTimeout(this.timer);  
		this.timer = null;  
	}  
}
03-09 16:57