跳到主要内容

useIdle

跟踪页面上的用户是否处于空闲状态。

Usage

实时编辑器

function Demo() {
  const isIdle = useIdle(3e3);

  return (
    <div>
      <div>User is idle: {isIdle ? "Yes 😴" : "Nope"}</div>
    </div>
  );
};

结果
Loading...

API

UseIdle

Returns

boolean: 是否处于空闲

Arguments

参数名描述类型默认值
ms检测时间number | undefined60e3
initialState初始值boolean | undefinedfalse
events监听的事件(keyof WindowEventMap)[] | undefined["mousemove","mousedown","resize","keydown","touchstart","wheel"]