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;  
    	}  
    }
相关推荐
中国胖子风清扬5 天前
GPUI 在 macOS 上编译问题排查指南
spring boot·后端·macos·小程序·rust·uni-app·web app
码云数智-园园6 天前
uni-app 实现物流进度跟踪功能:从 UI 到数据驱动的完整方案
ui·uni-app
予你@。7 天前
UniApp + Vue3 实现 Tab 点击滚动定位(微信小程序)
微信小程序·小程序·uni-app
游戏开发爱好者87 天前
完整教程:App上架苹果App Store全流程指南
android·ios·小程序·https·uni-app·iphone·webview
予你@。7 天前
uni-app progress 组件使用详解
uni-app
iOS阿玮7 天前
春节提审高峰来袭!App Store 审核时长显著延长。
uni-app·app·apple
2501_916007478 天前
ios上架 App 流程,证书生成、从描述文件创建、打包、安装验证到上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106328 天前
iPhone 文件管理,如何进行应用沙盒文件查看
android·ios·小程序·https·uni-app·iphone·webview
2501_915921439 天前
Fastlane 结合 AppUploader 来实现 CI 集成自动化上架
android·运维·ci/cd·小程序·uni-app·自动化·iphone
云游云记9 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue