From 95931b2b6e02d16a508d289165abb480c9fea65c Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 27 Jun 2024 20:45:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=A8=E6=88=B7=E6=97=A0=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E4=B8=80=E6=AE=B5=E6=97=B6=E9=97=B4=E5=90=8E=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E9=94=81=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/GlobalMask/index.vue | 31 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/components/GlobalMask/index.vue b/src/components/GlobalMask/index.vue index 9948332d..f250061d 100644 --- a/src/components/GlobalMask/index.vue +++ b/src/components/GlobalMask/index.vue @@ -14,23 +14,28 @@ const { t } = useI18n(); /**显示遮罩 */ const isVisible = computed(() => !['none', 'lock'].includes(maskStore.type)); -// 等待指定的时间后触发事件的函数 +// 用户无操作一段时间后进行锁屏 function idleTimeout(time: number, callback: Function) { if (time === 0) return; let timeoutId: any; - - function resetTimer() { - clearTimeout(timeoutId); - timeoutId = setTimeout(callback, time); + let idleTime = 0; + function resetIdleTime() { + idleTime = 0; } - - // 监听浏览器标签是否活动 - window.addEventListener('blur', useThrottleFn(resetTimer, 300)); - window.addEventListener('focus', useThrottleFn(resetTimer, 300)); - document.addEventListener('visibilitychange', useThrottleFn(resetTimer, 300)); - - // 初始化定时器 - resetTimer(); + // 监听用户活动事件 + document.addEventListener('mousemove', useThrottleFn(resetIdleTime, 1000)); + document.addEventListener('keydown', useThrottleFn(resetIdleTime, 1000)); + document.addEventListener('click', useThrottleFn(resetIdleTime, 1000)); + // 定时检查用户是否长时间无操作 + timeoutId = setInterval(() => { + idleTime += 1000; + console.log('idleTime', idleTime, idleTime >= time); + if (idleTime >= time) { + clearTimeout(timeoutId); + // 达到设定的无操作时间,执行锁屏操作 + callback(); + } + }, 1000); } /**组件实例挂载之后调用 */