在我的网页上设置超时会话时遇到问题

时间:2017-05-17 18:44:31

标签: javascript azure timer setinterval clearinterval

我正在开发一个应用程序,如果用户在页面上没有活动状态,则需要在一定时间后自行注销。我正在使用azure身份验证令牌,它会在一小时后到期。现在我在这里尝试设置两个计时器,第一个计时器将每隔一分钟运行一次,并将继续每次鼠标操作重置自己,第二个计时器应在58分钟无效后加载,显示会话中仅剩120秒。我无法获得所需的功能,第一个计时器在1分钟后运行,但同时它也会启动第二个计时器。

这是我的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>

2 个答案:

答案 0 :(得分:0)

我对网站需要一次相同的效果,如果有帮助,这里是代码。 (它设置为立即显示测试目的的提示)

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

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

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

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

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

	}
}

function resetlogout() {
	clearTimeout(autolog1);
	clearTimeout(autolog);
	autolog1 = setTimeout("logmeoutmsg()", 1);
	autolog = setTimeout("logmeout()", 10000);
	$("#logoutmsg").hide();	
}
<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;">
	<div style="width:760px;margin-left:20px;text-align:center;">
	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">
	<input style="color:#0000ff;cursor:pointer;" type="button" onclick="resetlogout();" value="Logout">
	</div>
</div>

答案 1 :(得分: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>