JS实现倒计时效果,并退出系统

背景:由于单点登录后,一直在本系统操作,可是门户体统的会话失效时间有30分钟,所以30分钟后,需要重新登录系统才可以进行操作。

方法:想过在本系统中的每个操作都先跟门户系统进行交互,渠道refreshToken,可是感觉任何一个操作都去请求refreshToken,会给系统带来问题,所以干脆就写一个JS来处理该问题。

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Js实现倒计时效果</title> 
<script language="javascript"> 

var timeLeft = 1 * 3 * 1000;//这里设定的时间是90秒 
function countTime(){ 
    if(timeLeft == 0){//这里就是时间到了之后应该执行的动作了,这里只是弹了一个警告框 
        var msg = "门户系统会话失效,需要重新登录,请确认?"; 
            if (confirm(msg)==true){ 
               var logoutUrl = "./oauthLogout.do";
               window.location.replace(logoutUrl);//页面刷新
               return; 
            }else{
               timeLeft = 1 * 3 * 1000;//这里设定的时间是90秒
            } 
    } 
    var startMinutes = parseInt(timeLeft / (60 * 1000), 10); 
    var startSec = parseInt((timeLeft - startMinutes * 60 * 1000)/1000); 
    document.getElementById('show').innerHTML = "剩余时间:" + startMinutes + "分钟" + startSec + ""; 
    timeLeft = timeLeft - 1000; 
    setTimeout('countTime()',1000); 
} 

</script> 
</head> 

<body onload="countTime()"> 
<div id="show"></div> 
</body> 
</html>

以上是例子,可以根据情况,稍作修改就可以解决相关问题了。

如果这篇文章对您有所帮助,请随便打赏一下作为鼓励,我会再接再厉的!!!

猜你喜欢

转载自www.cnblogs.com/zhangliang88/p/11275609.html