Просмотр исходного кода

【新增】优化搜索功能,添加防抖处理

- 在搜索输入中添加防抖处理,延迟 300ms 执行搜索
- 清除上一次搜索的定时器,避免不必要的重复搜索
- 保留原有的搜索逻辑,支持名称、拼音、作者、描述和 ID 的模糊搜索
PJ568 1 год назад
Родитель
Сommit
c32a2d9d5f
1 измененных файлов с 20 добавлено и 11 удалено
  1. 20 11
      index.html

+ 20 - 11
index.html

@@ -371,24 +371,33 @@
           translate.execute([appsGrid]);
         }
 
-        // 搜索功能
+        // 搜索功能 - 已添加防抖
+        let searchTimeout = null;
         searchInput.addEventListener("input", () => {
+          // 清除之前的定时器
+          if (searchTimeout) {
+            clearTimeout(searchTimeout);
+          }
+          
           const query = searchInput.value.toLowerCase().trim();
           if (!query) {
             renderApps(packages);
             return;
           }
 
-          const filtered = packages.filter(
-            (pkg) =>
-              pkg.name.toLowerCase().includes(query) ||
-              (pkg.pinyin && pkg.pinyin.includes(query)) || // 支持拼音搜索
-              pkg.author.toLowerCase().includes(query) ||
-              (pkg.description &&
-                pkg.description.toLowerCase().includes(query)) ||
-              pkg.id.toLowerCase().includes(query)
-          );
-          renderApps(filtered);
+          // 设置新的定时器,延迟执行搜索
+          searchTimeout = setTimeout(() => {
+            const filtered = packages.filter(
+              (pkg) =>
+                pkg.name.toLowerCase().includes(query) ||
+                (pkg.pinyin && pkg.pinyin.includes(query)) || // 支持拼音搜索
+                pkg.author.toLowerCase().includes(query) ||
+                (pkg.description &&
+                  pkg.description.toLowerCase().includes(query)) ||
+                pkg.id.toLowerCase().includes(query)
+            );
+            renderApps(filtered);
+          }, 300); // 300ms 防抖延迟
         });
 
         // 初始化