From 10a871fb2080289252706c927c5575c7a7cf9aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B4=9B=E5=B8=8C=E9=9B=85?= Date: Wed, 8 May 2024 13:53:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E9=99=A4async=E7=9A=84=E9=97=AD?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/mqtt/index.vue | 59 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/views/mqtt/index.vue b/src/views/mqtt/index.vue index b616cc1..e4feeb9 100644 --- a/src/views/mqtt/index.vue +++ b/src/views/mqtt/index.vue @@ -79,37 +79,36 @@ async function publish() { if (i >= list.length) i = 0 return list[i] } - ;(async () => { - let exit = false - jobCancel.value = () => { - exit = true - jobCancel.value = null - publishing.startTimestamp = 0 - } - publishing.startTime = Date.now() - publishing.count = list.length - // 发送数据的定时器 - const interval = setInterval(() => { - if (exit) { - clearInterval(interval) - return - } - const item = next() - publishing.lastTime = Date.now() - if (item[0]?.content[0]) { - publishing.lastTimestamp = item[0].content[0].timeStamp - if (publishing.startTimestamp === 0) { - publishing.startTimestamp = publishing.lastTimestamp - } - } - publishing.onlineCars = item[0]?.content?.length ?? 0 - publishing.index = i - publishing.percentage = Number(((publishing.index / (publishing.count | 1)) * 100).toFixed(2)) - const json = JSON.stringify(item) - mqttClient!.publish(topic, json, {}) - }, 100) - })().catch((e) => console.error(e)) + let exit = false + jobCancel.value = () => { + exit = true + jobCancel.value = null + publishing.startTimestamp = 0 + } + publishing.startTime = Date.now() + publishing.count = list.length + // 发送数据的定时器 + const interval = setInterval(() => { + if (exit) { + clearInterval(interval) + return + } + const item = next() + publishing.lastTime = Date.now() + if (item[0]?.content[0]) { + publishing.lastTimestamp = item[0].content[0].timeStamp + if (publishing.startTimestamp === 0) { + publishing.startTimestamp = publishing.lastTimestamp + } + } + publishing.onlineCars = item[0]?.content?.length ?? 0 + publishing.index = i + publishing.percentage = Number(((publishing.index / (publishing.count | 1)) * 100).toFixed(2)) + + const json = JSON.stringify(item) + mqttClient!.publish(topic, json, {}) + }, 100) }