fix:clear interval fail when close autoRefresh (#108)

This commit is contained in:
drgnchan 2022-06-15 10:06:53 +08:00 committed by GitHub
parent f12d7899e4
commit 7813c92673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View File

@ -34,6 +34,7 @@ interface AppState {
pageTitle: string;
version: string;
isMobile: boolean;
reloadTimer: Nullable<any>;
}
export const appStore = defineStore({
@ -53,6 +54,7 @@ export const appStore = defineStore({
pageTitle: "",
version: "",
isMobile: false,
reloadTimer: null,
}),
getters: {
duration(): Duration {
@ -173,6 +175,9 @@ export const appStore = defineStore({
this.version = res.data.data.version;
return res.data;
},
setReloadTimer(timer: any): void {
this.reloadTimer = timer;
},
},
});
export function useAppStoreWithOut(): any {

View File

@ -69,7 +69,7 @@ limitations under the License. -->
</div>
</template>
<script lang="ts" setup>
import { ref, reactive } from "vue";
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { useAppStoreWithOut } from "@/store/modules/app";
import timeFormat from "@/utils/timeFormat";
@ -78,9 +78,6 @@ import Selector from "@/components/Selector.vue";
const { t, locale } = useI18n();
const appStore = useAppStoreWithOut();
const state = reactive<{ timer: ReturnType<typeof setInterval> | null }>({
timer: null,
});
const lang = ref<string>(locale.value || "en");
const autoTime = ref<number>(6);
const auto = ref<boolean>(appStore.autoRefresh || false);
@ -101,10 +98,10 @@ const handleAuto = () => {
appStore.setAutoRefresh(auto.value);
if (auto.value) {
handleReload();
state.timer = setInterval(handleReload, autoTime.value * 1000);
appStore.setReloadTimer(setInterval(handleReload, autoTime.value * 1000));
} else {
if (state.timer) {
clearInterval(state.timer);
if (appStore.reloadTimer) {
clearInterval(appStore.reloadTimer);
}
}
};
@ -112,12 +109,12 @@ const changeAutoTime = () => {
if (autoTime.value < 1) {
return;
}
if (state.timer) {
clearInterval(state.timer);
if (appStore.reloadTimer) {
clearInterval(appStore.reloadTimer);
}
if (auto.value) {
handleReload();
state.timer = setInterval(handleReload, autoTime.value * 1000);
appStore.setReloadTimer(setInterval(handleReload, autoTime.value * 1000));
}
};
const setLang = (): void => {