uniapp 页面显示时开启定时,页面不显示时清除定时

uniapp yekong 352℃

uniapp 微信小程序要求进入页面时,要定时请求接口,不在当前页面时,清除定时,节约资源。
结合了uview提供的uni.$u.pages api获取页面路由进行页面判断,对当前路由判断

onShow() {
	var that = this;
	that.getdata()
	that.timer = setInterval(() => {
		that.getdata()
	}, 5000);
},
onUnload() {
	var that = this;
	clearInterval(that.timer);
	console.log('onHide')
	that.timer = null;
},
onHide() {
	var that = this;
	clearInterval(that.timer);
	console.log('onHide')
	that.timer = null;
},
getdata() {
	var that = this;
	if (uni.$u.pages()[uni.$u.pages().length - 1].route == 'pages/home/home') {
		this.$refs.loginReg.Login()
	} else {
		clearInterval(that.timer);
		that.timer = null;
	}
}

关联页面

uniapp uview 获取当前页面栈

喜欢 (0)