计数器在几秒钟内闲置jquery
我在空闲库中有这个功能,但我需要的是计算秒的动作时间,我的意思是活动时间(onclick,onscroll和按键)。计数器在几秒钟内闲置jquery
功能是:
(function() {
var minutes = false;
var interval = 1000;
var IDLE_TIMEOUT = 5;
var idleCounter = 0;
var counter=0;
document.onclick = document.onkeypress = function() {
idleCounter = 0;
setInterval(function() {
++counter;;
}, 1000);
};
window.setInterval(function() {
if (++idleCounter >= IDLE_TIMEOUT) {
alert(counter);
document.location.href = "SessionExpired.aspx";
}
}, interval);
}());
此功能将等待5秒,如果页面上没有动作,所以我会被重定向到SessionExpired.aspx。如果有行动,那么我们每秒钟都会做++。
我需要当这个计数器在几秒钟内。
谢谢。
你可以只复位定时器这里
var counter;
var counterSeconds;
document.onclick = document.onkeypress = function() {
idleCounter = 0; // Set counter to 0 on each keypress/page click
clearInterval(counter) // Every time they click, clear the old interval and start a new one below
counter = setInterval(function() { // assign the interval to a variable so we can clear it
if (idleCounter > IDLE_TIMEOUT) { // Check if they've idled too long
document.location.href = "SessionExpired.aspx"; // Redirect them if they have
}
++counterSeconds;
++idleCounter; // Should increment 1/sec depending on your computer.
}, 1000); // Ticks at 1000 milliseconds (1 Second)
};
我的柜台显示的是动作的次数,而不是我活动的次数。计数器给出例如201(这意味着201点击+按键动作)。 –
@SouheilDiab我更新了我的答案,我给你一个更好的解决方案来管理超时而不是使用多个超时 – Datsik
我需要的是每次点击或按键时启动计数器,如果我没有活动,我需要停止这个计数器这一页。 –
一个问题是,你开始新的间隔功能为每次点击或按键事件导致多个线程更新相同的变量。
您应该在事件之外启动一个间隔线程。
试试这个:
document.onclick = document.onkeypress = function() {
idleCounter = 0;
};
var activeTimer = setInterval(function() {
++counter;
}, interval);
var idleTimer = window.setInterval(function() {
if (++idleCounter >= IDLE_TIMEOUT) {
alert(counter);
document.location.href = "SessionExpired.aspx";
}
}, interval);
这不工作亲爱的: https://jsfiddle.net/kassabmo88/ayzbtydr/ –
它的工作,我离开闲置它警报5,并点击活动计时器得到提高。或者可能是我不明白你的问题。 –
这就是我想要的准确,我做到了:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
<script src="./e-lawyer/JS/idle.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>timertest</title>
<script language="javascript">
var it;
x = 0;
$(document).ready(function(){
$('.status').html('active');
});
function count() {
x+=1;
$('.usercount').html(x);
}
(function() {
var timeout = 2000;
$(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>
你能PLZ加什么问题,您都面临着代码的细节? –
当我提醒(计数器)时,我需要多少秒钟进入活动状态(点击和按键),但是计数器会显示201和200之类的奇怪值,持续2-5秒作为活动时间。 –