auto refresh

This commit is contained in:
Sirttas
2026-06-21 12:41:32 +02:00
parent 9edb0e6ce8
commit 9f6cc616ad
4 changed files with 48 additions and 10 deletions
+30
View File
@@ -0,0 +1,30 @@
import {onUnmounted, ref} from "vue";
const DEFAULT_INTERVAL = 1_000;
export const useAutoRefresh = (callback: () => void | Promise<void>, interval: number = DEFAULT_INTERVAL) => {
const active = ref(false);
let timer: ReturnType<typeof setInterval> | undefined;
const stop = () => {
if (timer) {
clearInterval(timer);
timer = undefined;
}
active.value = false;
};
const start = () => {
if (active.value) {
return;
}
active.value = true;
timer = setInterval(callback, interval);
};
const toggle = () => active.value ? stop() : start();
onUnmounted(stop);
return {active, start, stop, toggle};
};