Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(frontend): use setInterval instead of setTimeout chain in MkTime #10981

Merged
merged 11 commits into from
Jul 4, 2023
18 changes: 11 additions & 7 deletions packages/frontend/src/components/global/MkTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<script lang="ts" setup>
import isChromatic from 'chromatic/isChromatic';
import { onUnmounted } from 'vue';
import { onMounted, onUnmounted } from 'vue';
import { i18n } from '@/i18n';
import { dateTimeFormat } from '@/scripts/intl-const';

Expand Down Expand Up @@ -49,17 +49,21 @@ const relative = $computed<string>(() => {
let tickId: number;

function tick() {
now = props.origin ?? (new Date()).getTime();
tamaina marked this conversation as resolved.
Show resolved Hide resolved
const ago = (now - _time) / 1000/*ms*/;
const _now = (new Date()).getTime();
const ago = (_now - _time) / 1000/*ms*/;
const next = ago < 60 ? 10000 : ago < 3600 ? 60000 : 180000;

tickId = window.setTimeout(tick, next);
if (_now - now > next) {
now = _now;
}
tamaina marked this conversation as resolved.
Show resolved Hide resolved
}

if (props.mode === 'relative' || props.mode === 'detail') {
tick();
if (!invalid && (props.mode === 'relative' || props.mode === 'detail' || props.origin === null)) {
onMounted(() => {
tickId = window.setInterval(tick, 10000);
});
onUnmounted(() => {
window.clearTimeout(tickId);
if (tickId) window.clearInterval(tickId);
});
}
</script>