Browse Source

fixed paused time

famanoder 6 years ago
parent
commit
faffc71cee
1 changed files with 15 additions and 2 deletions
  1. 15 2
      src/packages/countdown/countdown.vue

+ 15 - 2
src/packages/countdown/countdown.vue

@@ -60,7 +60,9 @@ const countdownTimer = {
   name: 'CountDown',
   name: 'CountDown',
   data() {
   data() {
     return {
     return {
-      restTime: 0
+      restTime: 0,
+      p: 0,
+      _curr: 0
     }
     }
   },
   },
   props: {
   props: {
@@ -106,6 +108,15 @@ const countdownTimer = {
       return `${d > 0 && this.showDays? d + '天' + h: h}小时${m}分${s}秒`;
       return `${d > 0 && this.showDays? d + '天' + h: h}小时${m}分${s}秒`;
     }
     }
   },
   },
+  watch: {
+    paused(v, ov) {
+      if(!ov) {
+        this._curr = Date.now();
+      }else{
+        this.p += (Date.now() - this._curr);
+      }
+    }
+  },
   created() {
   created() {
     if(this.interval > 0) {
     if(this.interval > 0) {
       let _start = 0;
       let _start = 0;
@@ -117,13 +128,15 @@ const countdownTimer = {
       this.restTime = end - (start + diffTime);
       this.restTime = end - (start + diffTime);
       this.timer = setInterval(() => {
       this.timer = setInterval(() => {
         if(!this.paused) {
         if(!this.paused) {
-          let restTime = end - (new Date().getTime() + diffTime);
+          let restTime = end - (new Date().getTime() - this.p + diffTime);
           restTime -= 1000;
           restTime -= 1000;
           this.restTime = restTime;
           this.restTime = restTime;
           if(restTime < 0) {
           if(restTime < 0) {
             this.restTime = 0;
             this.restTime = 0;
             clearInterval(this.timer);
             clearInterval(this.timer);
           }
           }
+        }else{
+          // 暂停
         }
         }
       }, this.interval);
       }, this.interval);