Prechádzať zdrojové kódy

食堂菜单加入定时器

黄梓星 2 rokov pred
rodič
commit
c868e726b2

+ 25 - 2
src/views/canteen/foodScreen.vue

@@ -29,11 +29,17 @@ export default {
   data() {
     return {
       tableData: [],
-      nowDate: ''
+      nowDate: '',
+      intervalId: null
     }
   },
   created() {
     this.getList()
+    this.dataRefreh()
+  },
+  destroyed(){
+    // 在页面销毁后,清除计时器
+    this.clear();
   },
   methods: {
     getList() {
@@ -58,7 +64,24 @@ export default {
       }
       const nowDate = year + "-" + month + "-" + day;
       this.nowDate = nowDate
-    }
+    },
+   // 定时刷新数据函数
+   dataRefreh() {
+      // 计时器正在进行中,退出函数
+      if (this.intervalId != null) {
+        return;
+      }
+      // 计时器为空,操作
+      this.intervalId = setInterval(() => {
+        console.log("刷新" + new Date());
+        this.getList(); //加载数据函数
+      }, 1000*60*10);
+    },
+    // 停止定时器
+    clear() {
+      clearInterval(this.intervalId); //清除计时器
+      this.intervalId = null; //设置为null
+    },
   }
 }
 </script>

+ 24 - 4
src/views/canteen/menuScreen.vue

@@ -42,14 +42,17 @@ export default {
   data() {
     return {
       tableData: [],
-      nowDate: ''
+      nowDate: '',
+      intervalId: null
     }
   },
   created() {
-    this.getList()
+    this.getList();
+    this.dataRefreh()
   },
-  mounted() {
-
+  destroyed(){
+    // 在页面销毁后,清除计时器
+    this.clear();
   },
   methods: {
     getList() {
@@ -80,6 +83,23 @@ export default {
       const nowDate = year + "-" + month + "-" + day;
       this.nowDate = nowDate
     },
+   // 定时刷新数据函数
+   dataRefreh() {
+      // 计时器正在进行中,退出函数
+      if (this.intervalId != null) {
+        return;
+      }
+      // 计时器为空,操作
+      this.intervalId = setInterval(() => {
+        console.log("刷新" + new Date());
+        this.getList(); //加载数据函数
+      }, 1000*60*10);
+    },
+    // 停止定时器
+    clear() {
+      clearInterval(this.intervalId); //清除计时器
+      this.intervalId = null; //设置为null
+    },
   },
 }
 </script>