skyfffire 2 months ago
parent
commit
706332de74
1 changed files with 21 additions and 1 deletions
  1. 21 1
      monitor.html

+ 21 - 1
monitor.html

@@ -84,6 +84,7 @@
             let isAutoRefreshPaused = false;
             let allTasksData = {};
             let historyDataTable = null;
+            let isFetching = false; // <--- 在这里添加“刷新锁”变量
 
             const refreshButton = document.getElementById('refreshButton');
             const toggleAutoRefreshButton = document.getElementById('toggleAutoRefreshButton');
@@ -202,25 +203,44 @@
 
             // --- Main Logic Functions ---
             async function fetchData() {
+                // 检查锁:如果当前正在获取数据,则直接跳过本次刷新,防止冲突
+                if (isFetching) {
+                    console.log("刷新任务正在进行中,跳过此次周期。");
+                    return;
+                }
+
+                // 上锁:标记刷新任务开始
+                isFetching = true; 
+
                 try {
                     const [processingRes, historyRes] = await Promise.all([
                         fetch(`${API_BASE_URL}/processing`),
                         fetch(`${API_BASE_URL}/history`)
                     ]);
+                    
+                    // 检查网络请求是否成功
                     if (!processingRes.ok) throw new Error(`Processing fetch failed: ${processingRes.status}`);
                     if (!historyRes.ok) throw new Error(`History fetch failed: ${historyRes.status}`);
                     
                     const processingData = await processingRes.json();
                     const historyData = await historyRes.json();
 
+                    // 渲染是同步操作,在锁的保护下是安全的
                     renderProcessingTable(processingData);
                     renderHistoryDataTable(historyData);
                 } catch (error) {
                     console.error("获取数据失败:", error);
+                    // 即使出错,也要更新UI以反馈错误
                     document.getElementById('processing-list').innerHTML = `<tr><td colspan="5" class="text-center p-4 text-red-500">加载数据失败</td></tr>`;
-                    document.getElementById('history-table').innerHTML = `<tbody><tr><td colspan="5" class="text-center p-4 text-red-500">加载数据失败</td></tr></tbody>`;
+                    const historyTable = document.getElementById('history-table');
+                    if(historyDataTable) historyDataTable.destroy();
+                    historyTable.innerHTML = `<thead><tr><th>交易对</th><th>状态</th><th>利润</th><th>创建时间</th><th>操作</th></tr></thead><tbody><tr><td colspan="5" class="text-center p-4 text-red-500">加载数据失败</td></tr></tbody>`;
+                    
                     processingCountBadge.textContent = 'ERR';
                     historyCountBadge.textContent = 'ERR';
+                } finally {
+                    // 释放锁:无论成功还是失败,最后都要确保释放锁,以便下次刷新可以进行
+                    isFetching = false; 
                 }
             }
             function refreshAllData() { console.log("Refreshing data..."); fetchData(); }