鼠标/键盘不活动?如果激活则执行函数并且每秒只执行一次
我正在使用 PHP 和 jquery 制作一个用户计数脚本,我希望有一种方法来判断用户是否处于非活动状态。
$.mousemove(function(){
//get php to update time on user
});
但我应该如何设置它,以便它不会每次移动时更新,而是每 1 秒更新一次?像这样?
$.mousemove(function(){
//get php to update time on user
$.delay(1000);
});
然后我还将添加一个具有相同功能的按键功能,以便我还可以判断键盘是否处于活动状态。
I'm making a user count script with PHP and jquery and I would like to have a way of telling if the user is inactive.
$.mousemove(function(){
//get php to update time on user
});
but how should I set it so that it wouldn't update every time it moved but once every 1 sec? Like this?
$.mousemove(function(){
//get php to update time on user
$.delay(1000);
});
then I will also add a key up function with the same thing so that i can also tell if keyboard is active or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望这是不言自明的,并希望它能起作用!当用户移动鼠标时,这会立即通知服务器(假设服务器在一秒钟内没有收到通知),并且会定期通知服务器。
我们安排
activityNotification()
每秒运行一次(使用类似 jQuery 计时器 或setInterval(func, time)
函数),为了尽可能响应地处理以下时间线:立即
但通知服务器还为时过早
活动
ActivityNotification() 运行如下
已安排,看到我们有
我们尚未通知的活动
服务器关于,通知服务器
已计划,但没有通知服务器大约
代码:
请记住,并非所有用户都会启用 JavaScript,这将导致严重的电池耗尽移动设备。
Hopefully this is self explanatory, and hopefully it works! This notifies the server immediately when the user moves the mouse, assuming the server hasn't been notified in over a second, AND periodically.
We schedule
activityNotification()
to run every second (using something like jQuery timer or thesetInterval(func, time)
function), in order to handle the following timeline as responsively as possible:immediately
but too early to notify server of
activity
activityNotification() runs as
scheduled, sees that we have
activity we have not notified the
server about, notifies server
scheduled, but nothing to inform server about
Code:
And remember, not all users will have JavaScript enabled, and this will cause serious battery drain on mobile devices.