Skip to content
Back to skills

1339 Timeout Polling 63a998c2

ASecurity

> **错误 ID**: E002 > **频率**: 高 > **严重度**: 🔴 严重 ---

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
developmentjavascriptjavaapisecurity

Works with

  • api

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 1339-timeout-polling_63a998c2 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 1339 Timeout Polling 63a998c2?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 1339 Timeout Polling 63a998c2
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-1339-timeout-polling-63a998c2/badge)](https://www.skillsdirectory.com/skills/tools-only-1339-timeout-polling-63a998c2)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# 超时与轮询错误

> **错误 ID**: E002
> **频率**: 高
> **严重度**: 🔴 严重

---

## 📋 错误描述

**常见表现**:
- 轮询永不停止
- UI 永远显示 loading
- 用户只能刷新页面

**根本原因**:
- 未设置最大尝试次数
- 没有超时限制
- 缺少取消机制

---

## ❌ 错误示例

```javascript
// ❌ 错误:无限轮询
function pollScanStatus(scanId) {
  scanPollInterval = setInterval(async () => {
    const response = await fetch(`/api/security/scan/${scanId}/status`);
    const data = await response.json();

    if (data.status === 'completed' || data.status === 'failed') {
      clearInterval(scanPollInterval);
      updateUI(data);
    }
  }, 2000); // 每 2 秒轮询一次,但永不超时
}
```

---

## ✅ 正确做法

```javascript
// ✅ 正确:带超时的轮询
function pollScanStatus(scanId, maxAttempts = 30) {
  let attempts = 0;

  scanPollInterval = setInterval(async () => {
    attempts++;

    // 超时检查
    if (attempts > maxAttempts) {
      clearInterval(scanPollInterval);
      showError('扫描超时,请稍后重试');
      return;
    }

    try {
      const response = await fetch(`/api/security/scan/${scanId}/status`);
      const data = await response.json();

      if (data.status === 'completed' || data.status === 'failed') {
        clearInterval(scanPollInterval);
        updateUI(data);
      }
    } catch (error) {
      clearInterval(scanPollInterval);
      showError('查询失败: ' + error.message);
    }
  }, 2000);
}
```

---

## 🔍 关键改进

1. ✅ 设置 `maxAttempts` 限制
2. ✅ 添加 `try-catch` 处理网络错误
3. ✅ 超时后清理 interval
4. ✅ 提供友好的错误提示

---

## 📌 自检清单

- [ ] 轮询是否设置 `maxAttempts`?
- [ ] 是否有超时检查?
- [ ] 失败/超时后是否 `clearInterval`?
- [ ] 是否有错误处理和用户提示?

---

## 🎯 最佳实践

```javascript
// 推荐:使用 Promise 封装轮询逻辑
async function pollWithTimeout(pollFn, {
  interval = 2000,
  maxAttempts = 30,
  timeout = 60000
}) {
  const startTime = Date.now();

  for (let i = 0; i < maxAttempts; i++) {
    // 检查总超时时间
    if (Date.now() - startTime > timeout) {
      throw new Error('轮询超时');
    }

    const result = await pollFn();
    if (result.isDone) {
      return result.data;
    }

    await new Promise(resolve => setTimeout(resolve, interval));
  }

  throw new Error('达到最大尝试次数');
}
```

---

**返回**: [ERROR_CATALOG.md](../ERROR_CATALOG.md)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.