我正在用PHP和jquery制作一个用户统计脚本,我想有一种方法来告诉用户是否处于非活动状态。
$.mousemove(function(){
//get php to update time on user
});但是,我应该如何设置它,使它不会每次移动都更新,而是每1秒更新一次?是像这样吗?
$.mousemove(function(){
//get php to update time on user
$.delay(1000);
});然后,我还将添加一个具有相同功能的key up功能,以便我还可以判断键盘是否处于活动状态。
发布于 2011-05-24 20:16:20
希望这是不言而喻的,希望它能起作用!这会在用户移动鼠标时立即通知服务器,假设在超过一秒钟内没有通知服务器,并且是周期性的。
我们安排activityNotification()每秒运行一次(使用jQuery timer或setInterval(func, time) function之类的东西),以便尽可能响应地处理以下时间线:
代码:
//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,这将导致移动设备上的电池严重耗尽。
https://stackoverflow.com/questions/6110052
复制相似问题