这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
WakeLock.request()
方法用来获取屏幕唤醒锁定权限,防止屏幕变暗、关闭或展示屏幕保护程序。
语法
WakeLock.request(wakeLockType)
参数
wakeLockType
- 唤醒锁定类型,目前必须传
"screen"
。
返回值
决议为 WakeLockSentinel
的 Promise
。
异常
NotAllowedError
- 当唤醒锁定不可用的时候抛出,例如在:
- 由于屏幕唤醒锁定策略,Document 不允许使用屏幕唤醒锁定。
- Document 不完全激活。
- Document 被隐藏。
- User Agent 无法获取平台的唤醒锁定。
示例
以下示例获取屏幕唤醒锁定并在 10 分钟后释放它:
function tryKeepScreenAlive(minutes) { navigator.wakeLock.request("screen").then(lock => { setTimeout(() => lock.release(), minutes * 60 * 1000); }); } tryKeepScreenAlive(10);
规范
规范 | 状态 | Comment |
---|---|---|
Screen Wake Lock API | Editor's Draft | Initial specification. |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.