ServiceWorkerRegistration.unregister()

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

ServiceWorkerRegistration 接口的 unregister 方法用于取消对service worker的注册并返回一个 Promise。没有找到注册时,这个 promise 返回 false ,否则,不论取消成功与否都返回 true (当其他人在同一作用域调用了 ServiceWorkerContainer.register 可能取消失败)service worker 会在取消注册前完成一切正在进行的操作。

Note: 这一特性同样适用于 Web Workers.

语法

ServiceWorkerRegistration.unregister().then(function(boolean) {
});

参数

None.

返回

Promise 返回一个bool值表示 service worker 是否被取消注册。

例子

下面的简单例子中注册了一个service worker,然后立即取消了:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
    // registration worked
    console.log('Registration succeeded.');
    registration.unregister().then(function(boolean) {
      // if boolean = true, unregister is successful
    });
  }).catch(function(error) {
    // registration failed
    console.log('Registration failed with ' + error);
  });
};

规范

Specification Status Comment
Service Workers
ServiceWorkerRegistration.unregister()
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
unregister
Experimental
Chrome Full support 40Edge Full support 17
Full support 17
Full support 16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Firefox Android Full support 44Opera Android Full support 27Safari iOS Full support 11.3Samsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

参见