|
@@ -1,16 +1,29 @@
|
|
|
<template>
|
|
|
- <span>{{ lastTime }}</span>
|
|
|
+ <span>{{ realTime }}</span>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
export default {
|
|
|
- props: ["target", "onEnd"],
|
|
|
+ props: {
|
|
|
+ target: null,
|
|
|
+ onEnd: Function,
|
|
|
+ format: Function
|
|
|
+ },
|
|
|
data() {
|
|
|
return {
|
|
|
lastTime: this.initTime(),
|
|
|
timer: null
|
|
|
};
|
|
|
},
|
|
|
+ mounted() {
|
|
|
+ this.tick();
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ realTime() {
|
|
|
+ const format = this.format ? this.format : this.defaultFormat;
|
|
|
+ return format(this.lastTime);
|
|
|
+ }
|
|
|
+ },
|
|
|
methods: {
|
|
|
fixedZero(val) {
|
|
|
return val * 1 < 10 ? `0${val}` : val;
|
|
@@ -29,6 +42,31 @@ export default {
|
|
|
}
|
|
|
lastTime = targetTime - new Date().getTime();
|
|
|
return lastTime < 0 ? 0 : lastTime;
|
|
|
+ },
|
|
|
+ tick() {
|
|
|
+ const interval = 1000;
|
|
|
+ this.timer = setTimeout(() => {
|
|
|
+ if (this.lastTime < interval) {
|
|
|
+ clearTimeout(this.timer);
|
|
|
+ this.lastTime = 0;
|
|
|
+ if (this.onEnd) {
|
|
|
+ this.onEnd();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.lastTime -= interval;
|
|
|
+ this.tick();
|
|
|
+ }
|
|
|
+ }, interval);
|
|
|
+ },
|
|
|
+ defaultFormat(time) {
|
|
|
+ const hours = 60 * 60 * 1000;
|
|
|
+ const minutes = 60 * 1000;
|
|
|
+
|
|
|
+ const h = Math.floor(time / hours);
|
|
|
+ const m = Math.floor((time - h * hours) / minutes);
|
|
|
+ const s = Math.floor((time - h * hours - m * minutes) / 1000);
|
|
|
+
|
|
|
+ return `${this.fixedZero(h)}:${this.fixedZero(m)}:${this.fixedZero(s)}`;
|
|
|
}
|
|
|
}
|
|
|
};
|