uni-app 中使用定时器和取消定时器

uni-app 中使用定时器和清除定时器

uni-app 中我们应该如何使用定时器呢?在结束后我们该如何让清除定时器? 下面我们就来分享一下吧。

1、 定义一个

复制代码
data(){
    return{
        timer: null
    }
}

2、设置定时器

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

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;  
    	}  
    }
相关推荐
前端l2 小时前
uni‑app(Vue3+setup)开发微信小程序高频坑汇总
微信·uni-app
pan3035074795 小时前
uniapp 不用发版也可以做到版本更新
uni-app
00后程序员张7 小时前
使用Instruments工具深入分析iOS应用性能与启动时间优化
android·macos·ios·小程序·uni-app·cocoa·iphone
古韵8 小时前
小程序上传进度条,还要自己监听 onProgressUpdate 吗?
前端·javascript·uni-app
不如摸鱼去1 天前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·ui·微信小程序·前端框架·uni-app
anyup1 天前
uni-app 没有根组件?仅需几行代码实现全局 Toast 和 Modal
前端·架构·uni-app
2501_916007471 天前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone
AI_AGENT_DEV_AI1 天前
原生 APP(iOS / Android)的开发与上线
uni-app
小徐_23332 天前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·微信小程序·uni-app
skiyee2 天前
🔥 oiyo & unibest = 又新又好的 uniapp 模板
前端·uni-app