generated from VLADIMIR/template_frontend
26 lines
588 B
TypeScript
26 lines
588 B
TypeScript
import { onMounted, onUnmounted,ref } from 'vue';
|
|
|
|
export function useSimplePolling(callback: () => void, interval = 5000) {
|
|
const isActive = ref(true);
|
|
let timerId: number | null = null;
|
|
|
|
const start = () => {
|
|
isActive.value = true;
|
|
callback();
|
|
timerId = window.setInterval(callback, interval);
|
|
};
|
|
|
|
const stop = () => {
|
|
isActive.value = false;
|
|
if (timerId) {
|
|
clearInterval(timerId);
|
|
timerId = null;
|
|
}
|
|
};
|
|
|
|
onMounted(start);
|
|
onUnmounted(stop);
|
|
|
|
return { isActive, start, stop };
|
|
}
|