Skip to main content

取消渲染

warning

非常实验性的功能——随时可能出现漏洞和重大更改。 在 GitHub 上跟踪进度 并在 Discord 的 #web-renderer 通道中讨论。

renderMediaOnWeb()renderStillOnWeb() 都通过 AbortSignal API 支持取消。

🌐 Both renderMediaOnWeb() and renderStillOnWeb() support cancellation via the AbortSignal API.

使用 AbortController

🌐 Using AbortController

创建一个 AbortController 并将其 signal 传递给渲染函数:

🌐 Create an AbortController and pass its signal to the render function:

Cancel after timeout
import {renderMediaOnWeb} from '@remotion/web-renderer'; const abortController = new AbortController(); // Cancel after 10 seconds setTimeout(() => abortController.abort(), 10000); const {getBlob} = await renderMediaOnWeb({ signal: abortController.signal, composition, });

检测渲染是否被取消

🌐 Detecting if a render was cancelled

当渲染被取消时,会抛出错误。要区分用户发起的取消和实际错误,请检查信号是否已中止:

🌐 When a render is cancelled, an error is thrown. To distinguish between a user-initiated cancellation and an actual error, check if the signal was aborted:

Handle cancellation in catch block
import {renderMediaOnWeb} from '@remotion/web-renderer'; const abortController = new AbortController(); try { const {getBlob} = await renderMediaOnWeb({ signal: abortController.signal, composition, }); } catch (error) { if (abortController.signal.aborted) { // Render was cancelled by the user, handle gracefully console.log('Render was cancelled'); } else { // Handle actual errors throw error; } }

另请参阅

🌐 See also