当前位置:   article > 正文

uniapp 使用定时器和取消定时器

uniapp 使用定时器和取消定时器

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

1.1. 定义一个timer

data(){
    return{
        timer: null
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

1.2. 设置定时器

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

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;  
	}  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/221405
推荐阅读
相关标签
  

闽ICP备14008679号