BackgroundFetchRegistration:progress 事件
注意:此功能在 Web Workers 中可用。
當關聯的後臺獲取操作發生進度更新時,會觸發 BackgroundFetchRegistration 介面的 progress 事件。
實際上,當以下任一屬性返回新值時,都會觸發此事件:
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("progress", (event) => { })
onprogress = (event) => { }
事件型別
一個通用的 Event,沒有額外的屬性。
示例
以下示例演示瞭如何記錄下載進度。程式碼首先檢查註冊後臺獲取時是否提供了 downloadTotal。然後,根據 downloaded 屬性計算百分比。
js
bgFetch.addEventListener("progress", () => {
if (!bgFetch.downloadTotal) return;
const percent = Math.round(
(bgFetch.downloaded / bgFetch.downloadTotal) * 100,
);
console.log(`Download progress: ${percent}%`);
});
規範
| 規範 |
|---|
| Background Fetch # background-fetch-registration-events |
瀏覽器相容性
載入中…