guonan
2025-07-03 f37845dd0a787dd42bf6c72e923433f30fcd8cc3
src/views/left/KGSimOption/RealTimeSimulation.vue
@@ -223,13 +223,12 @@
// 用于记录上次数据条数
let lastDataLength = 0;
async function startPlay() {
  // 开始模拟前需要先保存方案
  updateSelectedGauges();
let pollingTimer = null; // 用于保存定时器引用
async function startPlay() {
  updateSelectedGauges();
  formData.geom = props.selectedArea;
  // 保存方案
  const resApi = await simAPIStore.addSimCheme(formData);
  const schemeId = resApi.data?.data?.id;
@@ -240,7 +239,6 @@
  EventBus.emit("close-selectArea");
  // 显示加载中提示
  const loadingMessage = ElMessage({
    type: "info",
    message: "正在启动模拟...",
@@ -249,31 +247,48 @@
  });
  try {
    // 启动模拟
    await getSimStart(schemeId);
    // 首次请求延迟 90s
    setTimeout(async () => {
    // 定义一个函数用于轮询获取数据
    const pollForResult = async () => {
      try {
        const res = await getSimresult(schemeId);
        console.log(res.data, "实时模拟 - 初始结果");
        console.log(res.data, "实时模拟 - 轮询结果");
        if (res.data.length > 0) {
        if (res.code === 200 && res.data.length > 0) {
          // 成功拿到数据
          loadingMessage.close();
          handleNewData(res.data, schemeId);
        }
          startPolling(schemeId);
        // 显示结果并开始轮询
        loadingMessage.close();
        startPolling(schemeId);
          // ✅ 清除定时器
          if (pollingTimer) {
            clearTimeout(pollingTimer);
            pollingTimer = null;
          }
        } else {
          // 数据无效,继续轮询
          pollingTimer = setTimeout(pollForResult, 10 * 1000);
        }
      } catch (error) {
        console.error("首次请求模拟结果失败", error);
        loadingMessage.close();
        console.error("请求模拟结果失败", error);
        pollingTimer = setTimeout(pollForResult, 10 * 1000); // 请求出错也继续轮询
      }
    }, 3 * 60 * 1000); // 1.5 分钟后第一次请求
    };
    // 首次延迟 2 分钟开始轮询
    pollingTimer = setTimeout(async () => {
      await pollForResult(); // 开始第一次轮询
    }, 3 * 60 * 1000); // 3分钟后第一次请求
  } catch (error) {
    loadingMessage.close();
    ElMessage.error("请求失败:" + (error.message || "未知错误"));
    console.error("调用 getSimStart 出错:", error);
    if (pollingTimer) {
      clearTimeout(pollingTimer);
      pollingTimer = null;
    }
  }
}