Skip to content

useWakeLock

Category
Export Size
727 B
Last Changed
15 minutes ago

响应式 屏幕唤醒锁定 API。提供了一种防止设备在应用程序需要持续运行时变暗或锁定屏幕的方法。

Demo

是否支持: false
激活状态: false

用法

js
import { useWakeLock } from '@vueuse/core'

const { isSupported, isActive, forceRequest, request, release } = useWakeLock()

当调用 request 时,如果文档是可见的,将会请求唤醒锁。否则,请求将会排队等待文档变得可见。如果请求成功,isActive 将会是 true。每当文档隐藏时,isActive 将会是 false

当调用 release 时,唤醒锁将会被释放。如果有排队的请求,将会被取消。

要立即请求唤醒锁,即使文档是隐藏的,请使用 forceRequest。请注意,如果文档是隐藏的,这可能会抛出错误。

类型声明

typescript
type WakeLockType = "screen"
export interface WakeLockSentinel extends EventTarget {
  type: WakeLockType
  released: boolean
  release: () => Promise<void>
}
export type UseWakeLockOptions = ConfigurableNavigator & ConfigurableDocument
/**
 * 响应式屏幕唤醒锁定 API。
 *
 * @see https://vueuse.org/useWakeLock
 * @param options
 */
export declare function useWakeLock(options?: UseWakeLockOptions): {
  sentinel: ShallowRef<WakeLockSentinel | null, WakeLockSentinel | null>
  isSupported: ComputedRef<boolean>
  isActive: ComputedRef<boolean>
  request: (type: WakeLockType) => Promise<void>
  forceRequest: (type: WakeLockType) => Promise<void>
  release: () => Promise<void>
}
export type UseWakeLockReturn = ReturnType<typeof useWakeLock>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Jelf
Anthony Fu
_Kerman
vuff
HannesOberreiter
wheat
koheing

变更日志

v11.0.0-beta.2 on 7/17/2024
5ca57 - fix: should delay wake lock request if document is hidden (#4055)

Released under the MIT License.