Javascript 以秒为单位的计数器空闲jquery

Javascript 以秒为单位的计数器空闲jquery,javascript,jquery,idle-timer,Javascript,Jquery,Idle Timer,我在空闲库中有这个函数,但我需要的是以秒为单位计算动作时间,我指的是活动时间(onclick、onscroll和keypress) 功能是: (function () { var minutes = false; var interval = 1000; var IDLE_TIMEOUT = 5; var idleCounter = 0; var counter=0; document.onclick = document.onkeypress = function () {

我在空闲库中有这个函数,但我需要的是以秒为单位计算动作时间,我指的是活动时间(onclick、onscroll和keypress)

功能是:

    (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)
};

这里的一个问题是,每次单击或按键事件都会启动新的间隔函数,这会导致多个线程更新同一个变量

您应该在事件外部启动一个间隔线程

试试这个:

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);

这正是我想要的,我做到了:

    <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>

计时试验
var-it;
x=0;
$(文档).ready(函数(){
$('.status').html('active');
});
函数计数(){
x+=1;
$('.usercount').html(x);
}
(功能(){
var超时=2000;
$(document.bind(“idle.idleTimer”,function()){
clearInterval(it);
$('.status').html('idle');
});
$(document.bind(“active.idleTimer”,function()){
它=设置间隔(计数,1000);
$('.status').html('active');
});
$.idleTimer(超时);
})(jQuery);

您能详细说明您在代码中面临的问题吗?当我发出警报(计数器)时,我需要活动的秒数(单击并按键),但计数器显示奇怪的值,如201和200,持续2-5秒作为活动时间。我的计数器显示的是操作数,而不是我活动的秒数。计数器给出了例如201(这意味着201次点击+按键操作)。@SouheilDiab I更新了我的答案,在管理超时而不是使用多次超时方面,我给您提供了一个更好的解决方案。我需要的是每次单击或按键时启动计数器,如果计数器在页面中不活动,我需要停止计数器。@SouheilDiab这正是我上面的代码所做的,每当有人与页面交互时,它都会按照您想要的方式运行,计时器被清除并重新启动,
idleCounter
重置回零,并在秒内开始计数,如果它超过它重定向的超时时间。如果我遗漏了什么,请让我知道。是的,这是正确的,亲爱的,但我需要秒的计数器值(计数器是指当我单击或按键时页面中的活动时间),它在空闲时对我起作用,警报5,单击活动计时器获得增加。也许我不明白你的问题。