Explorar o código

定时器处理

zhengkaixin hai 1 ano
pai
achega
ea21fa274d
Modificáronse 1 ficheiros con 24 adicións e 6 borrados
  1. 24 6
      pages/statistics/statistics.vue

+ 24 - 6
pages/statistics/statistics.vue

@@ -459,7 +459,7 @@
 				myBarChart: null,
 				myPieChart: null,
 				intervalId: null, // 用于存储间隔ID
-				intervalReady: true, // 用于存储间隔ID 
+				intervalReady: false, // 用于存储间隔ID 
 				list: [{
 						name: '上月',
 						value: "2"
@@ -541,10 +541,16 @@
 			// this.current=1
 			// this.getHomePageManage()
 
-		},
+		},
+		onUnload(){
+			this.clearTimer(); // 组件销毁前清除定时器
+			
+			console.log("组件销毁前清除定时器")
+		},
+		
 		beforeDestroy() {
 			this.clearTimer(); // 组件销毁前清除定时器
-			this.intervalReady = false;
+			
 			console.log("组件销毁前清除定时器")
 		},
 		
@@ -560,6 +566,15 @@
 				})
 				this.companyType= bl
 			}
+		},
+		onHide(){
+			this.intervalReady=false
+			this.clearTimer(); 
+		},
+		onShow(){
+			if(!this.intervalReady){
+				this.startInterval();
+			}
 		},
 		methods: {
 			switchBtnApiMethod(node,key){
@@ -741,10 +756,11 @@
 				}
 			},
 			startInterval() {
-				this.clearTimer(); // 组件销毁前清除定时器 
+				this.clearTimer(); // 组件销毁前清除定时器 
+				
+				
 				this.intervalId = setInterval(() => {
-					// 每隔5秒运行的代码  
-					// console.log('这段代码每隔5秒运行一次');
+				
 					this.getHomePageKwh(0, true)
 				}, 60000);
 			},
@@ -934,6 +950,8 @@
 
 					if (!interval) {
 						this.startInterval(); // 组件挂载后开始间隔  
+					}else{
+						this.intervalReady=true
 					}
 				}).catch(error => {
 					uni.showToast({