js IdleDetector 检测用户是否处于活动状态API

btn.addEventListener("click", async () => {
  try {
    const state = await Notification.requestPermission();
    if (state !== "granted") {
      // Need to request permission first.
      return console.log("Idle detection permission not granted.");
    }

    const controller = new AbortController();
    const signal = controller.signal;

    // 创建空闲检测器
    const idleDetector = new IdleDetector();

    // 设置一个事件侦听器,该侦听器在空闲状态更改时触发。
    idleDetector.addEventListener("change", () => {
      const uState = idleDetector.userState;  // 是否活动状态
      const sState = idleDetector.screenState; // 是否锁屏
      console.log(`Idle change: %s, %s.`, uState, sState);
    });

    // 启动空闲检测器。
    await idleDetector.start({
      threshold: 60000, // 最小值为60,000毫秒(1分钟)
      signal,
    });

    // 中断检测
    // controller.abort();
  } catch (error) {
    console.error("[IdleDetector] Error: %s", error.message);
  }
});
原文地址:https://www.cnblogs.com/ajanuw/p/13796610.html