From 5e95a1f7af841f10646133ad0cc155a2c5cea9fd Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 26 Jun 2022 03:12:58 +0900 Subject: refactor(client): extract interval logic to a composable function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit あと`onUnmounted`を`onMounted`内で呼んでいたりしたのを修正したりとか --- packages/client/src/scripts/use-interval.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 packages/client/src/scripts/use-interval.ts (limited to 'packages/client/src/scripts/use-interval.ts') diff --git a/packages/client/src/scripts/use-interval.ts b/packages/client/src/scripts/use-interval.ts new file mode 100644 index 0000000000..eb6e44338d --- /dev/null +++ b/packages/client/src/scripts/use-interval.ts @@ -0,0 +1,22 @@ +import { onMounted, onUnmounted } from 'vue'; + +export function useInterval(fn: () => void, interval: number, options: { + immediate: boolean; + afterMounted: boolean; +}): void { + let intervalId: number | null = null; + + if (options.afterMounted) { + onMounted(() => { + if (options.immediate) fn(); + intervalId = window.setInterval(fn, interval); + }); + } else { + if (options.immediate) fn(); + intervalId = window.setInterval(fn, interval); + } + + onUnmounted(() => { + if (intervalId) window.clearInterval(intervalId); + }); +} -- cgit v1.2.3-freya