优化useIdle
parent
e5745580c5
commit
b40e9a57a6
|
|
@ -1,7 +1,7 @@
|
||||||
export interface UseIdleOptions {
|
export interface UseIdleOptions {
|
||||||
/** 空闲超时时间(毫秒),默认值 10000 */
|
/** 空闲超时时间(毫秒),默认值 10000 */
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
/** 监听的事件列表,默认值 ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'wheel'] */
|
/** 监听的事件列表,默认值 ["mousemove", "mousedown", "resize", "keydown", "touchstart", "wheel"] */
|
||||||
events?: string[];
|
events?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,7 @@ import { useEffect, useState } from "react";
|
||||||
function useIdle(options = {}) {
|
function useIdle(options = {}) {
|
||||||
const {
|
const {
|
||||||
timeout = 10000,
|
timeout = 10000,
|
||||||
events = [
|
events = ["mousemove", "mousedown", "resize", "keydown", "touchstart", "wheel"],
|
||||||
"mousedown",
|
|
||||||
"mousemove",
|
|
||||||
"keypress",
|
|
||||||
"scroll",
|
|
||||||
"touchstart",
|
|
||||||
"wheel",
|
|
||||||
],
|
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
const [isIdle, setIsIdle] = useState(false);
|
const [isIdle, setIsIdle] = useState(false);
|
||||||
|
|
@ -21,29 +14,52 @@ function useIdle(options = {}) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let idleTimer;
|
let idleTimer;
|
||||||
|
|
||||||
// 重置空闲计时器
|
// 设置空闲状态
|
||||||
const resetTimer = () => {
|
const setIdle = () => {
|
||||||
setIsIdle(false);
|
setIsIdle(true);
|
||||||
clearTimeout(idleTimer);
|
|
||||||
idleTimer = setTimeout(() => setIsIdle(true), timeout);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 初始化计时器
|
// 重置为空闲前的状态
|
||||||
|
const setActive = () => {
|
||||||
|
setIsIdle(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 重置计时器
|
||||||
|
const resetTimer = () => {
|
||||||
|
clearTimeout(idleTimer);
|
||||||
|
idleTimer = setTimeout(setIdle, timeout);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化计时器(仅在非空闲状态下)
|
||||||
|
if (!isIdle) {
|
||||||
resetTimer();
|
resetTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 事件处理函数
|
||||||
|
const handleUserActivity = () => {
|
||||||
|
// 如果当前是空闲状态,用户操作后重置为活跃状态
|
||||||
|
if (isIdle) {
|
||||||
|
setActive();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 如果当前是活跃状态,重置计时器
|
||||||
|
resetTimer();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 添加事件监听器
|
// 添加事件监听器
|
||||||
events.forEach((event) => {
|
events.forEach((event) => {
|
||||||
window.addEventListener(event, resetTimer, { passive: true });
|
window.addEventListener(event, handleUserActivity, { passive: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
// 清理函数
|
// 清理函数
|
||||||
return () => {
|
return () => {
|
||||||
clearTimeout(idleTimer);
|
clearTimeout(idleTimer);
|
||||||
events.forEach((event) => {
|
events.forEach((event) => {
|
||||||
window.removeEventListener(event, resetTimer);
|
window.removeEventListener(event, handleUserActivity);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}, [timeout, events]);
|
}, [timeout, events, isIdle]);
|
||||||
|
|
||||||
return isIdle;
|
return isIdle;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue