1

Idle ライブラリにこの関数がありますが、アクション時間を秒単位で計算する必要があります。つまり、アクティブ時間 (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 にリダイレクトされます。アクションがある場合は、毎秒 ++counter を実行しています。

このカウンターが秒単位で必要な場合。

ありがとうございました。

4

3 に答える 3

1

タイマーをリセットするだけです

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)
};
于 2016-02-28T09:30:03.783 に答える
0

これは私がまさに望んでいたものであり、私はそれをやった:

    <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>
于 2016-02-28T10:33:13.987 に答える
0

ここでの問題の 1 つは、複数のスレッドが同じ変数を更新する原因となるクリックまたはキー押下イベントごとに新しい間隔関数を開始することです。

イベントの外で間隔スレッドを開始する必要があります。

これを試して:

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);
于 2016-02-28T09:46:15.547 に答える