我想当网页上的用户在指定的时间内没有任何活动时调用 js 函数。如果用户有活动,则重置超时。我试图搜索但找不到任何特别的东西。我熟悉 setTimeout() 和clearTimeout() 及其工作原理。我正在寻找的是在哪里/如何监视用户活动。有没有可以设置和清除计时器的事件?
谢谢。
编辑#1:
该网页有一个输入文本框和一个输入文本框。一键。这是一种常规的聊天页面。当我说没有用户活动时,我的意思是用户没有在文本框中输入任何内容或在指定的时间内没有按下任何按钮。 还有一点,它是针对基于触摸的智能手机设备的。
编辑 #2:
谢谢大家的建议。我已经根据提供的多个答案实施了解决方案。因此,我会对我认为有帮助的所有答案投赞成票,而不是接受一个答案。
I want to call a js function when there is no activity from user on the web page for specified amount of time. If there is activity from user then reset timeout. I tried to search but couldn't find anything in particular. I am familiar with setTimeout() and clearTimeout() and how they work. What I am looking for is where/how to monitor for user activity. Is there any event in which I can set and clear timer?
Thank you.
Edit #1:
This webpage has one input text box & one button. It's kind of regular chat page. When I say no user activity, I mean that the user has not typed anything in text box or has not pressed any button for specified amount of time. And one more thing that it is targeted for touch based smartphone devices.
Edit #2:
Thank you everyone for suggestions. I've implemented solution based on more than one answers provided. So I will give upvote to all answers that I've found helpful instead of accepting one as answer.
发布评论
评论(7)
编辑:出于某种原因不使用 jQuery?下面是一些(未经测试的)代码,应该是跨浏览器干净的(在某种程度上;例如,在 IE5 Mac 上不起作用;):
Edit: Not using jQuery for whatever reason? Here's some (untested) code that should be cross-browser clean (to a point; doesn't work on IE5 Mac, for example ;):
这需要一个反跳器:
像这样使用:
第一个用户操作(鼠标移动、单击或滚动)启动一个函数(附加到计时器),每次发生另一个用户操作时该函数都会重置。在经过指定的时间且没有任何操作后,主回调才会触发。
请注意,不需要全局标志或超时变量。全局范围仅接收您的去抖回调。 谨防需要维护全局状态的解决方案;在更大的应用程序中,它们将很难推理。
另请注意,此解决方案完全是通用的。请注意仅适用于您极其狭窄的用例的解决方案。
This calls for a debouncer:
Used like this:
The first user action (mousemove, click, or scroll) kicks off a function (attached to a timer) that resets each time another user action occurs. The primary callback does not fire until the specified amount of time has passed with no actions.
Note that no global flags or timeout variables are needed. The global scope receives only your debounced callback. Beware of solutions that require maintenance of global state; they're going to be difficult to reason about in the context of a larger application.
Note also that this solution is entirely general. Beware of solutions that apply only to your extremely narrow use case.
大多数 JavaScript 事件冒泡,因此您可以执行如下操作:
click
、mousemove
、keydown
等document
(或者可能是document.body
对于其中一些;我不记得这是否是一个问题)。clearTimeout
/setTimeout
重置计时器所以你最终会得到这样的结果:
请注意,你会遇到一些问题用上面的代码来解决:
addEventListener
,所以它在 IE6-8 中不起作用更重要的是让您了解自己可以做什么。
现在还有其他四个答案,但我已经全部输入了,所以:P
Most JavaScript events bubble, so you could do something like the following:
click
,mousemove
,keydown
, etc.)document
(or maybedocument.body
for some of them; I can't remember if that's an issue or not).clearTimeout
/setTimeout
So you'd end up with something like this:
Note that there are some issues you'd have to work out with the above code:
addEventListener
, so it won't work in IE6-8It's more to give you an idea of what you could do.
And now there are four other answers, but I've already typed it all up, so there :P
您希望在文档级别监视
mousemove
、keypress
、keydown
和/或click
等事件。编辑:这是一个智能手机应用程序,可以改变您想要监听的事件。考虑到您的文本框和按钮要求,我将监听
oninput
,然后将resetTimeout()
调用添加到按钮的点击处理程序中。You want to monitor events like
mousemove
,keypress
,keydown
, and/orclick
at the document level.Edit: This being a smartphone app changes what events you want to listen for. Given your textbox and button requirements, I'd listen to
oninput
and then add theresetTimeout()
call to the click handler for your button.像这样的东西:
IE:http://jsfiddle.net/acNfy/
右下框架中的活动将延迟回调。
Something like this:
IE: http://jsfiddle.net/acNfy/
Activity in the bottom right frame will delay the callback.
我正在使用一个漂亮的小“延迟”方法,我在 这个线程 中找到了
类似的用法
I'm using a nifty little 'delay' method for this that I found in this thread
use like
另外,请查看 Paul Irish 的 jQueryidleTimer 插件 (jquery.idle-timer.js)。它基于 Nicholas C. Zakas 的 使用 JavaScript 和 YUI 3 检测用户是否空闲 文章 (idle-timer.js)。
它着眼于与其他答案类似的事件,还有更多。
Also, take a look at jQuery idleTimer plugin from Paul Irish (jquery.idle-timer.js). It was based on Nicholas C. Zakas' Detecting if the user is idle with JavaScript and YUI 3 article (idle-timer.js).
It looks at similar events to the other answers, plus a few more.