---
title: Idle Detection API:用户空闲检测与权限策略
keywords:
- IdleDetector
- userState
- screenState
- 权限策略
- 空闲检测
description: 说明 Idle Detection 的用户与屏幕状态检测、权限与策略、适用场景与隐私治理,并提供示例与回退建议。
categories:
- 应用软件
- 编程开发
---
概述
Idle Detection API 提供检测用户与屏幕空闲的能力(如用户离开或屏幕锁定),需要权限与策略允许。适合自动保存、同步与后台任务调度等场景。
示例(实验性)
const detector = new IdleDetector()
await detector.start({ threshold: 60_000 })
detector.addEventListener('change', () => {
console.log(detector.userState, detector.screenState)
if (detector.userState === 'idle') scheduleBackgroundSync()
})
工程建议
- 权限与最小化:仅在必要场景启用;向用户透明说明用途。
- 调度策略:在空闲时进行非关键任务;避免打扰与资源浪费。
- 兼容:不支持时回退到基于交互超时的推断;记录效果。
参考与验证
- Chrome 平台文档(Idle Detection):https://developer.chrome.com/docs/web-platform/idle-detection/
- web.dev Idle Detection 指南:https://web.dev/articles/idle-detection

发表评论 取消回复