All effectsLoading & feedback

Debounced Autosave Status

防抖自动保存状态

A text edit moves through unsaved, saving, and saved after a quiet period.

Type, stop for a moment, then edit again during saving

Where to use it

Make background persistence legible in note, document, and settings editors. Place the status next to the document title or editor so users can check whether the latest change persisted before leaving.

Practical example

In a shared checklist, the header shows “Unsaved” while someone edits a label. After a short pause it switches to “Saving,” and it shows “Saved” only when that version is stored.

AI prompt

Add debounced autosave to [editor]. Persist a captured revision after a quiet period, keep an earlier result from clearing a newer dirty state, and announce transitions from unsaved to saving to saved.

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>Debounced Autosave Status</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; }
    }
    .autosave{display:grid;gap:5px}.autosave textarea{resize:none;font:inherit;line-height:1.4}.autosave-state::before{content:'';display:inline-block;height:7px;width:7px;border-radius:50%;background:#90e3bc;margin-inline-end:5px}.autosave-state[data-state=dirty]::before{background:#efc084}.autosave-state[data-state=saving]::before{background:#a29cec;animation:pending .75s ease-in-out infinite alternate}@keyframes pending{to{opacity:.25}}
  </style>
</head>
<body>
  <main class="demo">
    <div class="ui"><div class="surface autosave"><label for="note">Field note</label><textarea class="field" id="note" rows="3">Look for light.</textarea><div class="bar"><span class="muted autosave-state" data-state="saved" role="status">Saved</span><span class="mono revision">r1</span></div></div></div>
  </main>
  <script>
    const note = document.querySelector('#note');
    const status = document.querySelector('[role=status]');
    const revision = document.querySelector('.revision');
    const labels = {"dirty":"Unsaved changes","saving":"Saving…","saved":"Saved"};
    let generation = 1, timer = 0;
    function setState(state) { status.dataset.state = state; status.textContent = labels[state]; }
    note.addEventListener('input', () => {
      const snapshot = { id: ++generation, text: note.value };
      clearTimeout(timer); setState('dirty');
      timer = setTimeout(() => {
        setState('saving');
        setTimeout(() => {
          if (snapshot.id !== generation || note.value !== snapshot.text) return;
          revision.textContent = 'r' + snapshot.id; setState('saved');
        }, 620);
      }, 430);
    });
  </script>
</body>
</html>

Original example by UI Skills

Further examples