Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
use current date to calculate remaining time
Browse files Browse the repository at this point in the history
  • Loading branch information
chochihim committed Aug 23, 2019
1 parent d6b9cc5 commit ce1433f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/components/earnMDT/CountdownCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ export default {
},
},
data() {
const now = new Date();
const initialRemainingTime =
this.initialRemainingTime > 0 ? this.initialRemainingTime : 0;
return {
remainingTime: this.initialRemainingTime,
nowSeconds: Math.round(now.getTime() / 1000),
remainingTime: initialRemainingTime,
intervalId: null,
};
},
Expand Down Expand Up @@ -96,7 +100,11 @@ export default {
},
watch: {
initialRemainingTime: function(val) {
this.remainingTime = val;
if (val > 0) {
const now = new Date();
this.nowSeconds = Math.round(now.getTime() / 1000);
this.remainingTime = val;
}
},
},
beforeDestroy() {
Expand All @@ -106,8 +114,19 @@ export default {
},
created() {
this.intervalId = setInterval(() => {
const now = new Date();
const nowSeconds = Math.round(now.getTime() / 1000);
if (this.remainingTime > 0) {
this.remainingTime -= 1;
const nextRemainingTime =
this.remainingTime - (nowSeconds - this.nowSeconds);
if (nextRemainingTime > 0) {
this.remainingTime = nextRemainingTime;
} else {
this.remainingTime = 0;
}
this.nowSeconds = nowSeconds;
}
}, 1000);
},
Expand Down

0 comments on commit ce1433f

Please sign in to comment.