Javascript jquery处于空闲和活动状态的问题:当活动更新mysql上的useronline时间戳时
我只想在人们不空闲的时候更新useronline数据库上的时间戳(键盘鼠标在一段时间内处于活动状态——我正在使用jquery插件),问题是我无法清除函数的间隔,因此在下面的测试脚本中,当它开始计数时,它从不停止,甚至当它再次激活时,它也会启动另一个计数器,使它的计数速度加倍。我该怎么停止计时器?它就像计数器闲置时从未启动过一样,所以它无法找到它来停止它 在真正的脚本中,计数器将是$.get()-用于更新mysql表的php。因为这个原因,我使用的是间隔,否则每次鼠标移动都会出现间隔,对吗?这将加载服务器 要测试,只需在6秒钟内不移动鼠标,然后看到空闲文本,然后移动鼠标,它将开始计数;6秒钟后,当处于非活动状态时,它将再次进入空闲状态Javascript jquery处于空闲和活动状态的问题:当活动更新mysql上的useronline时间戳时,javascript,jquery,mysql,timer,idle-timer,Javascript,Jquery,Mysql,Timer,Idle Timer,我只想在人们不空闲的时候更新useronline数据库上的时间戳(键盘鼠标在一段时间内处于活动状态——我正在使用jquery插件),问题是我无法清除函数的间隔,因此在下面的测试脚本中,当它开始计数时,它从不停止,甚至当它再次激活时,它也会启动另一个计数器,使它的计数速度加倍。我该怎么停止计时器?它就像计数器闲置时从未启动过一样,所以它无法找到它来停止它 在真正的脚本中,计数器将是$.get()-用于更新mysql表的php。因为这个原因,我使用的是间隔,否则每次鼠标移动都会出现间隔,对吗?这将加
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="js/jquery_plugin_timer2.js"></script>
<script src="https://github.com/paulirish/jquery-idletimer/raw/master/jquery.idle-timer.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>timertest</title>
<script language="javascript">
$(document).ready(function(){
$('.status').html('active');
});
x = 0;
function count() {
x+=1;
$('.usercount').html(x);
}
(function() {
var timeout = 6000;
var it;
$(document).bind("idle.idleTimer", function() {
clearInterval(it);
$('.status').html('idle');
});
$(document).bind("active.idleTimer", function() {
var it = setInterval(count, 1000);
$('.status').html('active');
});
$.idleTimer(timeout);
})(jQuery);
</script>
</head>
<body>
<div class="status" style="border:1px dashed black; width:500px; height:50px;"></div>
<div class="usercount"style="border:1px dashed black; width:500px; height:50px;"></div>
</body>
</html>
计时试验
$(文档).ready(函数(){
$('.status').html('active');
});
x=0;
函数计数(){
x+=1;
$('.usercount').html(x);
}
(功能(){
var超时=6000;
var-it;
$(document.bind(“idle.idleTimer”,function()){
clearInterval(it);
$('.status').html('idle');
});
$(document.bind(“active.idleTimer”,function()){
var it=设置间隔(计数,1000);
$('.status').html('active');
});
$.idleTimer(超时);
})(jQuery);
计时试验
var-it;
$(文档).ready(函数(){
$('.status').html('active');
});
x=0;
函数计数(){
x+=1;
$('.usercount').html(x);
}
(功能(){
var超时=6000;
$(document.bind(“idle.idleTimer”,function()){
clearInterval(it);
$('.status').html('idle');
});
$(document.bind(“active.idleTimer”,function()){
它=设置间隔(计数,1000);
$('.status').html('active');
});
$.idleTimer(超时);
})(jQuery);
计时试验
var-it;
$(文档).ready(函数(){
$('.status').html('active');
});
x=0;
函数计数(){
x+=1;
$('.usercount').html(x);
}
(功能(){
var超时=6000;
$(document.bind(“idle.idleTimer”,function()){
clearInterval(it);
$('.status').html('idle');
});
$(document.bind(“active.idleTimer”,function()){
它=设置间隔(计数,1000);
$('.status').html('active');
});
$.idleTimer(超时);
})(jQuery);
如果您的问题在于计时,那是因为您没有正确地清除间隔。正在设置并在未定义或超出范围的情况下使用的变量。如果您的问题是计时,则是因为您没有清除正确的间隔。在未定义或超出范围的情况下设置和使用的变量。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="js/jquery_plugin_timer2.js"></script>
<script src="https://github.com/paulirish/jquery-idletimer/raw/master/jquery.idle-timer.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>timertest</title>
<script language="javascript">
var it;
$(document).ready(function(){
$('.status').html('active');
});
x = 0;
function count() {
x+=1;
$('.usercount').html(x);
}
(function() {
var timeout = 6000;
$(document).bind("idle.idleTimer", function() {
clearInterval(it);
$('.status').html('idle');
});
$(document).bind("active.idleTimer", function() {
it = setInterval(count, 1000);
$('.status').html('active');
});
$.idleTimer(timeout);
})(jQuery);
</script>
</head>
<body>
<div class="status" style="border:1px dashed black; width:500px; height:50px;"></div>
<div class="usercount"style="border:1px dashed black; width:500px; height:50px;"></div>
</body>
</html>