Browse Source

日常开发

linxuxiang 4 months ago
parent
commit
737939f2fa
1 changed files with 23 additions and 1 deletions
  1. 23 1
      src/views/sunshineRun/index.vue

+ 23 - 1
src/views/sunshineRun/index.vue

@@ -53,6 +53,7 @@ const { proxy } = getCurrentInstance() as any;
 const router = useRouter();
 
 const data = reactive<any>({
+  timerManager: {},//计时器管理
   reportList: [],//排行榜
   date: "",//当前时间
   unit: "",//单位
@@ -60,6 +61,7 @@ const data = reactive<any>({
 });
 
 const {
+  timerManager,
   reportList,
   date,
   unit,
@@ -70,7 +72,7 @@ const {
  * 更新时间
 */
 const setDate = () => {
-  setInterval(() => {
+  timerManager.value.currentTime = setInterval(() => {
     date.value = proxy?.$utils.getDate();
   }, 1000)
 };
@@ -125,6 +127,25 @@ const confirmExit = async () => {
   }
 };
 
+/**
+ * 清空定时任务
+*/
+const getClearTimer = (data?: any) => {
+  if (data) {
+    //清除指定
+    clearInterval(timerManager.value[data])
+    timerManager.value[data] = null;
+  } else {
+    //清除全部
+    for (let key in timerManager.value) {
+      if (timerManager.value.hasOwnProperty(key)) {
+        clearInterval(timerManager.value[key])
+        timerManager.value[key] = null;
+      }
+    }
+  }
+};
+
 onBeforeMount(() => {
   //获取当前时间
   setDate();
@@ -143,6 +164,7 @@ onMounted(() => {
 })
 
 onBeforeUnmount(() => {
+  getClearTimer();
 })
 </script>