鼠标/键盘不活动?如果激活则执行函数并且每秒只执行一次

发布于 2024-11-09 09:55:20 字数 350 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2024-11-16 09:55:20

希望这是不言自明的,并希望它能起作用!当用户移动鼠标时,这会立即通知服务器(假设服务器在一秒钟内没有收到通知),并且会定期通知服务器。

我们安排 activityNotification() 每秒运行一次(使用类似 jQuery 计时器setInterval(func, time) 函数),为了尽可能响应地处理以下时间线:

  • 0毫秒:用户移动鼠标,服务器通知
    立即
  • 657 ms:用户按下按键,
    但通知服务器还为时过早
    活动
  • 1000 毫秒:
    ActivityNotification() 运行如下
    已安排,看到我们有
    我们尚未通知的活动
    服务器关于,通知服务器
  • 2000 毫秒:activityNotification() 运行如下
    已计划,但没有通知服务器大约
  • 2124 毫秒:用户移动鼠标,服务器立即收到通知,因为自上次通知服务器以来已经过去了 1.124 秒

代码:

//Track the last activity you saw
var lastActivity = 0;

//Remember the last time you told the server about it
var lastNotified = 0;

//Determines how frequently we notify the server of activity (in milliseconds)
var INTERVAL = 1000;

function rememberActivity() {
    lastActivity = new Date().getTime();

    activityNotification();
}

function activityNotification() {
    if(lastActivity > lastNotified + INTERVAL) {
        //Notify the server
        /* ... $.ajax(); ... */

        //Remember when we last notified the server
        lastNotified = new Date().getTime();
    }
}

setInterval('activityNotification()', INTERVAL);

$.mousemove(function() {
    //Remember when we last saw mouse movement
    rememberActivity();
});

$.keyup(function() {
    //Remember when we last saw keyboard activity
    rememberActivity();
});

请记住,并非所有用户都会启用 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 the setInterval(func, time) function), in order to handle the following timeline as responsively as possible:

  • 0 ms: user moves mouse, server notified
    immediately
  • 657 ms: user presses key,
    but too early to notify server of
    activity
  • 1000 ms:
    activityNotification() runs as
    scheduled, sees that we have
    activity we have not notified the
    server about, notifies server
  • 2000 ms: activityNotification() runs as
    scheduled, but nothing to inform server about
  • 2124 ms: user moves mouse, server notified immediately as it's been 1.124 seconds since the server was last notified

Code:

//Track the last activity you saw
var lastActivity = 0;

//Remember the last time you told the server about it
var lastNotified = 0;

//Determines how frequently we notify the server of activity (in milliseconds)
var INTERVAL = 1000;

function rememberActivity() {
    lastActivity = new Date().getTime();

    activityNotification();
}

function activityNotification() {
    if(lastActivity > lastNotified + INTERVAL) {
        //Notify the server
        /* ... $.ajax(); ... */

        //Remember when we last notified the server
        lastNotified = new Date().getTime();
    }
}

setInterval('activityNotification()', INTERVAL);

$.mousemove(function() {
    //Remember when we last saw mouse movement
    rememberActivity();
});

$.keyup(function() {
    //Remember when we last saw keyboard activity
    rememberActivity();
});

And remember, not all users will have JavaScript enabled, and this will cause serious battery drain on mobile devices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文