summaryrefslogtreecommitdiff
path: root/src/client/app/common/views/components/timer.vue
blob: a3c4f01b77cb46fc671561ed6fb9681bc50f80a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<template>
<time class="mk-time">
	{{ hh }}:{{ mm }}:{{ ss }}
</time>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
	props: {
		time: {
			type: [Date, String],
			required: true
		}
	},
	data() {
		return {
			tickId: null,
			hh: null,
			mm: null,
			ss: null
		};
	},
	computed: {
		_time(): Date {
			return typeof this.time == 'string' ? new Date(this.time) : this.time;
		}
	},
	created() {
		this.tick();
		this.tickId = setInterval(this.tick, 1000);
	},
	destroyed() {
		clearInterval(this.tickId);
	},
	methods: {
		tick() {
			const now = new Date().getTime();
			const start = this._time.getTime();
			const ago = Math.floor((now - start) / 1000);

			this.hh = Math.floor(ago / (60 * 60)).toString().padStart(2, '0');
			this.mm = Math.floor(ago / 60).toString().padStart(2, '0');
			this.ss = (ago % 60).toString().padStart(2, '0');
		}
	}
});
</script>