Jelajahi Sumber

fix: 首页定时器优化

liujt 1 Minggu lalu
induk
melakukan
2e730db0f9
2 mengubah file dengan 19 tambahan dan 13 penghapusan
  1. 4 6
      pages/home/home.vue
  2. 15 7
      pages/home/homeNew.vue

+ 4 - 6
pages/home/home.vue

@@ -226,9 +226,12 @@
 		},
 		onUnload() {
 			uni.$off('scancodedate')
+			clearTimeout(this.timer);
+
 		},
 		onHide() {
 			uni.$off('scancodedate')
+			clearTimeout(this.timer);
 		},
 		data() {
 			return {
@@ -282,12 +285,6 @@
 				}
 			})
 		},
-		onHide() {
-			clearTimeout(this.timer);
-		},
-		onUnload() {
-			clearTimeout(this.timer);
-		},
 
 
 		methods: {
@@ -312,6 +309,7 @@
 						this.$refs.mouldRef.getModeInventoryCountList())
 				this.$refs.sparePartRef && this.$refs.sparePartRef.getData()
 				this.timer = setTimeout(() => {
+					console.log('timer~~~home')
 					this.init()
 				}, 12000)
 			},

+ 15 - 7
pages/home/homeNew.vue

@@ -148,11 +148,16 @@
 		onLoad() {
 		},
 		onUnload() {
+			this.stopTimer()
 			uni.$off('scancodedate')
 		},
 		onHide() {
+			this.stopTimer()
 			uni.$off('scancodedate')
 		},
+		beforeDestroy() {
+			this.stopTimer()
+		},
 		mounted() {
 			this.init()
 			this.getHomeData()
@@ -220,16 +225,16 @@
 			})
 			this.getNoticeDocumentList()
 		},
-		onHide() {
-			clearTimeout(this.timer);
-		},
-		onUnload() {
-			clearTimeout(this.timer);
-		},
+
 
 
 		methods: {
-			
+			// 停止定时轮询
+			stopTimer() {
+				console.log('stopTimer')
+				this._destroyed = true
+				clearTimeout(this.timer)
+			},
 			// 刷新 manage 子组件数据(供父组件 tab 切换时调用)
 			refreshManage() {
 				this.$refs.manageRef?.refreshData()
@@ -270,7 +275,10 @@
 					(this.$refs.mouldRef.getBoatInventoryCountList(),
 						this.$refs.mouldRef.getModeInventoryCountList())
 				this.$refs.sparePartRef && this.$refs.sparePartRef.getData()
+				if (this._destroyed) return
 				this.timer = setTimeout(() => {
+					console.log('init timeout')
+					if (this._destroyed) return
 					this.init()
 				}, 12000)
 			},