流式内容的暂停与取消
Streaming Response Controls
逐段呈现正在到达的内容,可暂停、继续和终止本次输出。
适用场景
让长文本或列表在传输未结束时就可查看。 部分结果已经能用、用户又需要控制阅读节奏时,可以为逐段到达的内容提供暂停和停止。
应用示例
访谈转录界面收到一句就追加一句。暂停时已显示的文字仍可阅读,点「停止」结束接收并保留屏幕上的转录稿。
AI 提示词
让【流式响应】逐步显示。分批追加完整片段,暂停时保留已显示内容,停止后释放读取资源,只播报状态变化,不逐段朗读。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>流式内容的暂停与取消</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; }
}
.stream{display:grid;gap:4px}.stream-text{height:61px;overflow:auto;background:#25223e;border-radius:5px;padding:5px 6px;font:11px/1.35 ui-monospace,monospace;white-space:pre-wrap}.stream[data-reading] .stream-text::after{content:'▍';color:#c5f3eb;animation:stream-pulse 1s linear infinite}@keyframes stream-pulse{50%{opacity:.25}}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="surface stream"><p class="muted" role="status">就绪</p><div class="stream-text" tabindex="0" aria-label="流式内容"></div></div><div class="actions"><button class="button primary start" type="button">开始</button><button class="button pause" type="button" disabled>暂停</button><button class="button stop" type="button" disabled>停止</button></div></div>
</main>
<script>
const text = document.querySelector('.stream-text');
const stage = document.querySelector('.stream');
const status = document.querySelector('[role=status]');
const start = document.querySelector('.start');
const pause = document.querySelector('.pause');
const stop = document.querySelector('.stop');
const chunks = ["正在检查输入…","正在生成提纲…","正在准备预览…","检查完成。"];
const labels = {"reading":"正在接收…","paused":"已暂停","resume":"继续","pause":"暂停","stopped":"已停止","complete":"已完成"};
let timer = 0, index = 0, paused = false;
function finish(message) { clearInterval(timer); start.disabled = false; pause.disabled = true; stop.disabled = true; pause.textContent = labels.pause; status.textContent = message; stage.removeAttribute('data-reading'); }
function schedule() { stage.setAttribute('data-reading', ''); status.textContent = labels.reading; timer = setInterval(() => { const atEnd = text.scrollHeight - text.scrollTop - text.clientHeight < 5; text.textContent += (index ? '\n' : '') + chunks[index++]; if (atEnd) text.scrollTop = text.scrollHeight; if (index === chunks.length) finish(labels.complete); }, 510); }
start.addEventListener('click', () => { index = 0; paused = false; text.textContent = ''; start.disabled = true; pause.disabled = false; stop.disabled = false; schedule(); });
pause.addEventListener('click', () => { paused = !paused; if (paused) { clearInterval(timer); stage.removeAttribute('data-reading'); status.textContent = labels.paused; } else schedule(); pause.textContent = paused ? labels.resume : labels.pause; });
stop.addEventListener('click', () => finish(labels.stopped));
</script>
</body>
</html>UI Skills 原创示例