jsp+js完成用户一定时间未操作就跳到登录页面

时间:2022-02-20 17:13:18
		<%
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
			
			UserInfo ui = UserInfo.getUserInfo(request);
			//如果获取不到用户对象,跳转回登录页面
			if(ui==null){
				request.getRequestDispatcher("/WEB-INF/jsp/nosso.jsp").forward(request,response);
			}  %>

  

var lastTime = new Date().getTime();
        var currentTime = new Date().getTime();
        var timeOut =60 * 1000*10; //设置超时时间: 10分	  
        $(function(){
            $(document).mouseover(function(){
                lastTime = new Date().getTime(); //更新操作时间
            });
        });
       	    function testTime(){
            currentTime = new Date().getTime(); //更新当前时间
            if(currentTime - lastTime > timeOut){ //判断是否超时
               // console.log("超时");
               var base="<%=basePath+"nosso.do"%>";
                window.location.href = base;
               	            }
        }
        /* 定时器  间隔1秒检测是否长时间未操作页面  */
        window.setInterval(testTime, 1000);