All effectsLoading & feedback

Retry Backoff Countdown

退避重试倒计时

An operation shows bounded retry delays, attempt numbers, and an explicit cancel path.

Start the sample retry cycle or cancel while waiting

Where to use it

Explain that a safe background operation is recovering from transient failure. Show both the next retry time and the attempt limit so failures outside the user’s control have a clear outcome.

Practical example

A weather dashboard loses contact with its data feed. It shows “Attempt 2 of 4” and a short countdown, then retries when time is up unless the user cancels.

AI prompt

Give [retryable request] a capped exponential retry schedule with jitter. Display time and attempt count, allow cancellation at any point, and retry only transient failures or idempotent calls.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Retry Backoff Countdown</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
    .ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
    .ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
    .ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
    .ui .muted { color: #cecae9; font-size: 11px; }
    .ui .center { text-align: center; }
    .ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
    .ui p { margin: 0; }
    .ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
    .ui .button:hover { background: #4e4976; }
    .ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
    .ui .primary:hover { background: #e3fff9; }
    .ui .button:disabled { opacity: .6; cursor: default; }
    .ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
    .ui .field::placeholder { color: #c2bcdd; }
    .ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
    .ui button { font: inherit; }
    .ui [hidden] { display: none !important; }
    .ui progress { accent-color: #b8ebe0; }
    @media (prefers-reduced-motion: reduce) {
      .ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
    }
    .ui progress{width:100%;height:8px}.surface{display:grid;gap:5px}
  </style>
</head>
<body>
  <main class="demo">
    <div class="ui"><div class="surface"><div class="bar"><strong>Cloud sync</strong><span class="mono attempt">0 / 3</span></div><p class="muted" role="status">Ready to sync</p><progress value="0" max="100" aria-label="Delay elapsed"></progress></div><div class="actions"><button type="button" class="button primary run">Start sync</button><button type="button" class="button cancel" disabled>Cancel</button></div></div>
  </main>
  <script>
    const run = document.querySelector('.run');
    const cancel = document.querySelector('.cancel');
    const indicator = document.querySelector('.attempt');
    const status = document.querySelector('[role=status]');
    const progress = document.querySelector('progress');
    let ticker = 0, attempt = 0;
    function end(message) { clearInterval(ticker); run.disabled = false; cancel.disabled = true; status.textContent = message; }
    function tryAgain() {
      attempt += 1; indicator.textContent = attempt + ' / 3';
      if (attempt === 3) { progress.value = 100; end("Sync recovered"); return; }
      const delay = 1000 * 2 ** (attempt - 1); const deadline = performance.now() + delay;
      let displayed = -1;
      function update() {
        const seconds = Math.max(0, deadline - performance.now());
        progress.value = 100 * (1 - seconds / delay);
        const remaining = Math.ceil(seconds / 1000);
        if (remaining !== displayed) { displayed = remaining; status.textContent = "Retry in " + remaining + 's'; }
        if (!seconds) { clearInterval(ticker); tryAgain(); }
      }
      ticker = setInterval(update, 80); update();
    }
    run.addEventListener('click', () => { attempt = 0; run.disabled = true; cancel.disabled = false; tryAgain(); });
    cancel.addEventListener('click', () => end("Canceled"));
  </script>
</body>
</html>

Original example by UI Skills

Further examples