ServiceWorkerGlobalScope:sync 事件
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
备注: 此特性仅在 Service Worker 中可用。
当页面(或 worker)使用 SyncManager 注册的事件正在运行,并且一旦网络连接可用时,将触发 ServiceWorkerGlobalScope 接口的 sync 事件。
此事件不能取消,也不会冒泡。
语法
像 addEventListener() 一样在方法中使用事件名称,或设置事件处理器属性。
js
addEventListener("sync", (event) => {});
onsync = (event) => {};
事件类型
SyncEvent。其继承自 ExtendableEvent 和 Event。
事件属性
自其父级 ExtendableEvent 和 Event 继承属性。
SyncEvent.tag只读-
返回此
SyncEvent的开发人员定义的标识符。 SyncEvent.lastChance只读-
如果用户代理在当前尝试之后不再进行进一步的同步尝试,则返回
true。
示例
以下示例显示了如何在 Service Worker 中响应同步事件。
js
self.addEventListener("sync", (event) => {
if (event.tag === "sync-messages") {
event.waitUntil(sendOutboxMessages());
}
});
你也可以通过 onsync 属性设置事件处理器:
js
self.onsync = (event) => {
// ...
};
规范
| Specification |
|---|
| Web Background Synchronization> # dom-serviceworkerglobalscope-onsync> |
浏览器兼容性
Loading…