Pārlūkot izejas kodu

fix心跳interval

Michael Wang 6 gadi atpakaļ
vecāks
revīzija
ccb2aa6b3a
1 mainītis faili ar 11 papildinājumiem un 6 dzēšanām
  1. 11 6
      src/features/OnlineExam/Examing/RemainTime.vue

+ 11 - 6
src/features/OnlineExam/Examing/RemainTime.vue

@@ -17,17 +17,16 @@ export default {
   async mounted() {
     this.heartbeatErrorNum = 0;
     this.getRemainTimeFromServer();
-    this.intervalA = setInterval(() => {
+    this.heartbeatInterval = setInterval(() => {
       this.getRemainTimeFromServer();
     }, 60 * 1000);
-    this.intervalB = setInterval(
+    this.remainTimeInterval = setInterval(
       () => (this.remainTime = this.remainTime - 1000),
       1000
     );
   },
   beforeDestroy() {
-    clearInterval(this.intervalA);
-    clearInterval(this.intervalB);
+    this.clearIntervals();
   },
   methods: {
     ...mapMutations(["setShouldSubmitPaper"]),
@@ -43,12 +42,14 @@ export default {
         this.heartbeatErrorNum = 0;
       } else {
         this.heartbeatErrorNum++;
-        if (this.heartbeatErrorNum === 10) {
+        if (this.heartbeatErrorNum >= 10) {
           // 心跳异常5次则退出考试
           this.$Modal.error({
             title: "网络连接异常",
             content: "退出考试",
             onOk: () => {
+              clearInterval(this.heartbeatInterval);
+              clearInterval(this.remainTimeInterval);
               this.$router.push("/login/" + localStorage.getItem("domain"));
             }
           });
@@ -56,6 +57,10 @@ export default {
         }
         setTimeout(() => this.getRemainTimeFromServer(), 10 * 1000);
       }
+    },
+    clearIntervals() {
+      clearInterval(this.heartbeatInterval);
+      clearInterval(this.remainTimeInterval);
     }
   },
   computed: {
@@ -65,7 +70,7 @@ export default {
   },
   watch: {
     remainTime(val) {
-      if (val < 0) {
+      if (val !== null && val < 0) {
         this.setShouldSubmitPaper();
       }
     }