新增useIdle

master
LiuJiaNan 2025-12-12 16:22:49 +08:00
parent c048814217
commit d851f578da
2 changed files with 62 additions and 0 deletions

11
hooks/useIdle/index.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
export interface UseIdleOptions {
/** 空闲超时时间(毫秒),默认值 10000 */
timeout?: number;
/** 监听的事件列表,默认值 ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'wheel'] */
events?: string[];
}
/**
*
*/
export default function useIdle(options?: UseIdleOptions): boolean;

51
hooks/useIdle/index.js Normal file
View File

@ -0,0 +1,51 @@
import { useEffect, useState } from "react";
/**
* 检测用户是否处于空闲状态
*/
function useIdle(options = {}) {
const {
timeout = 10000,
events = [
"mousedown",
"mousemove",
"keypress",
"scroll",
"touchstart",
"wheel",
],
} = options;
const [isIdle, setIsIdle] = useState(false);
useEffect(() => {
let idleTimer;
// 重置空闲计时器
const resetTimer = () => {
setIsIdle(false);
clearTimeout(idleTimer);
idleTimer = setTimeout(() => setIsIdle(true), timeout);
};
// 初始化计时器
resetTimer();
// 添加事件监听器
events.forEach((event) => {
window.addEventListener(event, resetTimer, { passive: true });
});
// 清理函数
return () => {
clearTimeout(idleTimer);
events.forEach((event) => {
window.removeEventListener(event, resetTimer);
});
};
}, [timeout, events]);
return isIdle;
}
export default useIdle;