From 1d2c5284e2b7caeb3475481fb8c2ec332d35d1e4 Mon Sep 17 00:00:00 2001 From: betaqi <3188864257@qq.com> Date: Thu, 11 Sep 2025 19:14:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20tab=E5=A4=84=E4=BA=8E=E9=9D=9E=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E7=8A=B6=E6=80=81=E4=B8=8B=20setInterval=20=E5=AE=9E?= =?UTF-8?q?=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/transferData.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/stores/transferData.ts b/src/stores/transferData.ts index 5364f1d..02af979 100644 --- a/src/stores/transferData.ts +++ b/src/stores/transferData.ts @@ -4,6 +4,7 @@ import type { IOnlineDevice, IUpFirmwareStatus } from '@/views/stationData/type' import ZMQWorker from '@/composables/useZMQJsonWorker' import { getSubTopic, type SubMsgData } from '@/utils/zmq' import { getDeviceTopic } from '@/views/stationData/utils' +import dayjs from "dayjs"; export const useTransferDataStore = defineStore('transfer', () => { const subDevices = getSubTopic('client', 'status', 'transfer') @@ -25,14 +26,17 @@ export const useTransferDataStore = defineStore('transfer', () => { const checkDeviceStatusInterval = ref() function checkDeviceStatus() { - checkDeviceStatusInterval.value = setInterval(() => { - const now = Date.now(); - devicesMap.forEach((device: IOnlineDevice, sn) => { - if (now - device.lastUpdated > 5500) { - device.status = '离线'; - } - }); - }, 1000); + checkDeviceStatusInterval.value = setInterval(checkDeviceStatusFn, 100); + } + + const checkDeviceStatusFn = () => { + const now = Date.now(); + devicesMap.forEach((device: IOnlineDevice, sn) => { + // console.log(device, dayjs(now).format('HH:mm:ss'), dayjs(device.lastUpdated).format('HH:mm:ss'), now - device.lastUpdated) + if (now - device.lastUpdated > 5500) { + device.status = '离线'; + } + }); } function formatSizeFromKB(num: number): string { @@ -88,6 +92,7 @@ export const useTransferDataStore = defineStore('transfer', () => { onMounted(() => { worker.subscribe(getDeviceTopic, getSubDevicesCb) checkDeviceStatus() + document.addEventListener('visibilitychange', checkDeviceStatusFn) }) const route = useRoute() @@ -95,6 +100,7 @@ export const useTransferDataStore = defineStore('transfer', () => { watch(() => route.path, (val) => { if (!['/station/data-transfer', '/station'].includes(val)) { clearInterval(checkDeviceStatusInterval.value) + document.removeEventListener('visibilitychange', checkDeviceStatusFn) worker.unsubscribe(subDevices) } })