This commit is contained in:
zk
2023-12-19 10:46:50 +08:00
parent 8cb7d4baf8
commit 9b5be1af25
11 changed files with 76 additions and 58 deletions
+19 -5
View File
@@ -1,14 +1,18 @@
import { onMounted, onUnmounted, ref } from 'vue';
import { isFunction } from '/@/utils/is.ts';
export function useRefresh(time = 3000, callback) {
/**
* @desc 定时刷新
* @param time
* @param callback
*/
export function useRefresh(time = 3000, callback?: Function) {
const refreshState = ref(false);
const timer = ref(null);
function setRefreshState() {
timer.value = setInterval(() => {
refreshState.value = !refreshState.value;
callback();
}, time);
startRefresh();
}
function stopRefresh() {
@@ -16,6 +20,15 @@ export function useRefresh(time = 3000, callback) {
timer.value = null;
}
function startRefresh() {
timer.value = setInterval(() => {
refreshState.value = !refreshState.value;
if (callback && isFunction(callback)) {
callback();
}
}, time);
}
onMounted(() => {
if (timer.value != null) {
timer.value = null;
@@ -29,5 +42,6 @@ export function useRefresh(time = 3000, callback) {
return {
refreshState,
stopRefresh,
startRefresh,
};
}