【问题标题】:page not redirecting after session expires会话到期后页面不重定向
【发布时间】:2013-05-20 09:51:54
【问题描述】:

我做了一个简单的页面,当会话到期时移动到另一个页面。我已将时间设置为 50 秒,之后它会移动到一个页面但不幸的是它没有移动,我无助地知道哪里出了问题。如果您能解决我的问题,我将不胜感激

function x(){
       var timeOut =<%=session.getAttribute("login")%>;

var checkTimeout;

checkTimeOut = function(){
    if(timeOut==null || timeOut==""){
        window.location.replace("failedSession.jsp");
//document.getElementById("b").innerHTML="session timed out";
        // redirect to timeout page
    }else{


        window.setTimeout(checkTimeOut, 1000); // check once per second
    }}
checkTimeOut(); // this is where you insert checkTimeOut function so that when you call x(), this will execute in the end.
}

【问题讨论】:

  • 如果您在这行 JavaScript 的末尾添加一个分号以便与其他代码匹配,您会发现您的代码更易于阅读: var timeOut = ;

标签: java jsp session servlets


【解决方案1】:

您的代码有问题:

您的代码中没有任何地方调用checkTimeOut();。这就是为什么它没有被执行。您应该通过将alert 放入函数中来检查函数的执行。正如@rgeorge 所建议的那样,可能在body onload 期间执行此操作。

注意:您的逻辑看起来不错。正如@Mark 所建议的那样,还有在javascript 语句 之后放置分号的习惯。

答案已更新:

1) 不要使用function namevariable 名称相同。例如:x。拥有独一无二的名字。这是矛盾的。

2) 你的x 总是大于lastActivity + timeOut,因为x 没有增加(7 > 1+1)。这意味着else 条件永远不会执行。这意味着永远不会调用checkTimeOut

3) 你的函数checkTimeOut() 本身永远不会执行,因为它永远不会从任何地方调用。你只是在执行x()。这不足以调用checkTimeOut()

答案已更新:

这样做:

<script type="text/javascript">
function x(){
       var timeOut =1;
var lastActivity = 1;
var x=7;

var checkTimeout;

checkTimeOut = function(){
    if(x > lastActivity + timeOut){
document.getElementById("b").innerHTML=x;
        // redirect to timeout page
    }else{
        window.setTimeout(checkTimeOut, 1000); // check once per second
    }}
checkTimeOut(); // this is where you insert checkTimeOut function so that when you call x(), this will execute in the end.
}

    </script>

您的代码适合我。 DIV 值更改为 7。这就是你的代码所做的 >> http://jsfiddle.net/hmHP9/

【讨论】:

  • 感谢您的回复,我一直在尝试以另一种方式,因此忘记发布原始代码。好吧,我已经编辑了我的帖子,但它仍然无法正常工作
  • 我是 manullay 提供值并检查但它没有显示 x
  • 是的,否则条件永远不会执行,所以 document.getElementById("b").innerHTML=x;在 if 条件下将执行,它会打印 x 的值但不打印
  • 我的朋友,你的函数checkTimeOut() 本身永远不会执行,因为它永远不会从任何地方调用。你只是在执行x()。这不足以调用checkTimeOut()
  • 看到我试过了,但这也不起作用
【解决方案2】:

您粘贴的代码最初似乎没有调用 checktimeout 函数。这也适用于您修改后的代码。它只是定义函数 checktimeout()。

我会添加一个 onload 函数调用到第一次调用 checktime out 的 body 标记。

function startTimeoutCheck() {
    window.setTimeout(checkTimeout, 1000);
}

<body onload="startTimeoutCheck()">
    ....
</body>

checktimeout 变量显然应该在上述函数的作用域(全局)内。

【讨论】:

  • 感谢您的回答,我已经编辑了我的帖子,但它仍然无法正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多