闲置/不活动 60 秒后重定向用户?
如何在我的网站上使用 JavaScript 在用户处于非活动状态 60 秒后将其重定向到 /logout
页面?
我知道设置计时器或使用元刷新标记很简单:但我只想重定向不活动的用户,而不是中断某人的活动会话/使用。
这可以用 JavaScript 实现吗?
How can I use JavaScript on my site to redirect the user to a /logout
page after 60 seconds of inactivity?
I know setting a timer or using a meta refresh tag is straightforward: but I only want to redirect inactive users, not disrupt someone's active session/usage.
Is this possible with JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要使用具有不必要的 Kbytes 的插件,您所需要的只是一个像这样的简单函数
(请参阅注释中的说明):
如果您想重定向到主页(通常位于
/
),将'/logout'
更改为'/'
:如果要重新加载/刷新当前页面,只需更改
'/logout '
在上面的代码中location.href
:Instead of using a plugin with unnecessary Kbytes, all you need is a simple function like this
(see explanation in comments):
If you want to redirect to the home page (usually at
/
), change'/logout'
to'/'
:If you want to reload/refresh the current page, simply change
'/logout'
in the code above tolocation.href
:我相信您正在寻找这样的东西:
http://paulirish.com/2009/jquery-idletimer-plugin/< br>
如果您自己编写代码,则需要捕获鼠标和键盘事件,并在发生这些事件后重新启动计时器。如果计时器达到阈值或从阈值倒计时到 0,您可以重置页面的 URL。
I belive you are looking for something like this:
http://paulirish.com/2009/jquery-idletimer-plugin/
If you were to code that yourself, you would need to capture mouse and keyboard events and restart your timer after any of these events. If the timer ever reaches the threshold or counts down to 0 from the threshold you can reset the URL of the page.
该插件还有一个更新版本。
它将能够在整个文档或单个元素上触发空闲事件。例如,将鼠标悬停在某个元素上 x 秒,它会触发一个事件。当用户再次激活时会触发另一个事件。
此空闲事件将允许您在给定的不活动时间后重定向用户。
支持的活动: mousemove keydown wheel DOMMouseScroll mousewheel mousedown touchstart touchmove MSPointerDown MSPointerMove
https://github.com/thorst/jquery-空闲定时器
There is also a more up-to-date version of the plugin.
It will be able to fire idle event on entire document or single elements. For example mouse over some element for x seconds and it fires an event. Another event is fired when user becomes active again.
This idle event will allow you to redirect user after given time of inactivity.
Supported activity: mousemove keydown wheel DOMMouseScroll mousewheel mousedown touchstart touchmove MSPointerDown MSPointerMove
https://github.com/thorst/jquery-idletimer
每当用户登录、单击某项或移动鼠标时设置计时器。您可以维护 localStorage、sessionStorage 或任何全局变量来跟踪空闲时间。
之后,每隔 10、20、30 或 60 秒(根据您的选择)继续从
setInterval()
之类的函数中调用以下函数,以检查该时间限制是否已过期。或者,您可以在用户尝试交互时调用该函数来检查其空闲时间是否超过阈值。您可以使用 cookie 来执行相同的操作。
Set a timer whenever a user logs in, clicks something or moves mouse. You can maintain a localStorage, sessionStorage or any global variable to keep track of the idle time.
After that, keep calling the following function from within something like
setInterval()
every 10,20,30 or 60 seconds (as per your choice) to check if that time limit has expired. Or you can call the function whenever a user tries to interact to check if his idle time has exceeded the threshold.You can use cookies to do the same.