2017-05-17 4 views
0

私は、ユーザがページ上でアクティブでない場合、一定時間後にログアウトする必要のあるアプリケーションに取り組んでいます。私は空白の認証トークンを使用していて、1時間で期限切れです。ここで私は2つのタイマーをセットアップしようとしています。最初のタイマーは1分ごとに実行され、すべてのマウスアクションでそれ自体をリセットし続け、2番目のタイマーは58分の休止後にロードして、 。私は機能を望んで得ることができません、最初のタイマーは1分後に実行されますが、同時にそれはまた2番目のタイマーのキック。それは場合に役立ちます私のウェブページでタイムアウトセッションを設定する際に問題がある

ここに私のJavascriptコードは、私が一度サイトで同じ効果を必要と..

<script> 

    function timerModal() { 
     var count = 120; 
     console.log("This has started"); 
     var counter = setInterval(timer, 1000); //1000 will run it every 1 second 

     function timer() { 

      count = count - 1; 
      if (count <= 0) { 

       $(this).mousemove(function (e) { 
        count = 120; 
       }); 
       $(this).keypress(function (e) { 
        count = 120; 
       }); 
       clearInterval(counter); 
       //vmsWebUtils.signOut(); //counter ended, do something here 
       return; 
      } 

      document.getElementById("timer").innerHTML = count + " "; // watch for spelling 
      console.log(count); 
     } 


    } 

    var idleTime = 0; 

    $(document).ready(function() { 
     //Increment the idle time counter every minute. 

     var idleInterval = setInterval(timerIncrement, 60000); // 1 minute 
     //Zero the idle timer on mouse movement. 
     $(this).mousemove(function (e) { 
      idleTime = 0; 
     }); 
     $(this).keypress(function (e) { 
      idleTime = 0; 
     }); 
    }); 

    function timerIncrement() { 
     idleTime = idleTime + 1; 
     if (idleTime => 57) { // 57 minutes 
      $("#sessionTimeout").show(); 
       timerModal(); 
     } 
     console.log(idleTime); 
    } 


</script> 
+0

ここをクリックしてください:http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly – couzzi

答えて

0

(すぐにテスト目的のためにプロンプ​​トを表示するように設定されています)私はいくつかの修正を加えた私の自己は、ここで変更したコードだそれを修正しました! 完璧に動作します!

<script> 
    var idleTime = 0; 
    var idleInterval = setInterval(timerIncrement, 60000); // 1 minute 
     $(this).mousemove(function (e) { 
      idleTime = 0; 
     }); 
     $(this).keypress(function (e) { 
      idleTime = 0; 
     }); 

    function timerIncrement() { 
     idleTime = idleTime + 1; 
     console.log(idleTime); 
     if (idleTime > 2) { // 57 minutes 
      clearInterval(idleInterval); 
      timerModal(); 
      console.log("hello"); 
     } 
     console.log(idleTime); 
    } 

    function timerModal() { 
     var count = 120; 
     console.log("This has started"); 
     var counter = setInterval(timer, 1000); //1000 will run it every 1 second 
     function timer() { 
      count = count - 1; 
      if (count <= 0) { 
       clearInterval(counter); 

       vmsWebUtils.signOut(); //counter ended, do something here 
       return; 
      } 
      document.getElementById("timer").innerHTML = count + " "; // watch for spelling 
      console.log(count); 
     } 
    } 

</script> 
0

だ、ここにコードがあります。

$('input').keypress(function(e) { 
 
    if (e.which == 13) { 
 

 
     $(this).next('input').focus(); 
 
     e.preventDefault(); 
 
    } 
 
\t resetlogout(); 
 
}); 
 
$('textarea').keypress(function(e) { 
 
\t resetlogout(); 
 
}); 
 

 
var autolog1 = setTimeout("logmeoutmsg()", 1); 
 
var autolog = setTimeout("logmeout()", 10000); 
 

 
function logmeout() { 
 
\t window.location.href = "index.php?logout=1"; 
 
} 
 

 
function logmeoutmsg() { 
 
\t $("#logoutmsg").show(); \t 
 
\t var count=10; 
 
\t var counter=setInterval(timer, 1000); //1000 will run it every 1 second 
 
\t timer(); 
 
\t function timer() 
 
\t { 
 
\t $("#counterdown").html(count); 
 
\t count=count-1; 
 
\t if (count <= 0) 
 
\t { 
 
\t \t clearInterval(counter); 
 
\t \t return; 
 
\t } \t 
 

 
\t } 
 
} 
 

 
function resetlogout() { 
 
\t clearTimeout(autolog1); 
 
\t clearTimeout(autolog); 
 
\t autolog1 = setTimeout("logmeoutmsg()", 1); 
 
\t autolog = setTimeout("logmeout()", 10000); 
 
\t $("#logoutmsg").hide(); \t 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="logoutmsg" style="display:none;position:fixed;left:50%;top:100px;margin-left:-400px;border:1px solid #2e2e2e;width:800px;background:#eedbba;padding:10px 0px;"> 
 
\t <div style="width:760px;margin-left:20px;text-align:center;"> 
 
\t You will be logged out in <div style="display:inline-block;" id="counterdown"></div> seconds.<br><input style="color:#0000ff;cursor:pointer;" type="button" onclick="resetlogout();" value="Cancel"> 
 
\t <input style="color:#0000ff;cursor:pointer;" type="button" onclick="resetlogout();" value="Logout"> 
 
\t </div> 
 
</div>

関連する問題