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 |
浏览器兼容性
加载中…