关于JS定时器判断cookie是否超时的问题(在线等,急求)

yezhubaobao 2014-02-14 03:07:31
主要是实现这样一个功能,页面打开后,如果一段时间没有操作,就清楚Cookie然后提示他登陆超时然后自动刷新到登陆页面
下面贴下代码,我写了个JS,在每个模板页里面都引用了,但是只有首页弹出登陆超时,其他都不会,不知道为什么。
$(document).ready(function () {
setInterval("Timer()", 62000);
})

function Timer() {
var strUrl = "Customer/TimeOver?time="+Math.random;
$.ajax({
type: "POST",
url: strUrl,
success: function (data) {
if (data.msg == "true") {
alert("登陆超时,请重新登陆");
window.parent.location.href = "/Account/LogOn";
}
}
});
}



下面是后台代码
public ActionResult TimeOver()
{
if (LoginOut(User.Identity.Name))
{
FormsAuthentication.SignOut();
return JsonGet(new { success = true, msg = "true" });
}
else
{
return JsonGet(new { success = false, msg = "false" });
}
}


请教各位大神,在线等,急求
...全文
576 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
风一样的大叔 2014-02-19
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js读取cookie测试</title>
<link href="css/08nzfh-style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head>

<body>

<div class="personality">
<script type="text/javascript">
//<![CDATA[
function   setCookie(name,value) 
{ 
    var   Days   =   30;   //此   cookie   将被保存   30   天 
    var   exp     =   new   Date();         //new   Date( "December   31,   9998 "); 
    exp.setTime(exp.getTime()   +   Days*24*60*60*1000); 
    document.cookie   =   name   +   "= "+   escape(value)   + ";expires= "+   exp.toGMTString(); 
} 

function getCookie2(objName){//获取指定名称的cookie的值
   var arrStr = document.cookie.split("; ");
   for(var i = 0;i < arrStr.length;i ++){
    var temp = arrStr<i>.split("=");
    if(temp[0] == objName) return unescape(temp[1]);
   } 
}

var _b = false;
//setCookie("mycookie","mycookie");
 if(typeof(getCookie2("mycookie2"))   !=   'undefined')   
 {   
   alert(typeof(getCookie2("mycookie2")));
   _b = true; 
 } 
 alert(_b);
 if(_b){
 	document.write('<iframe id="iframe_email" name="iframe_email" frameborder="0" scrolling="no" src="http://localhost:8008/vote1.4/test_8.jsp" height="100%" width="100%"></iframe>');
 }else{
 	document.write('<iframe src="#" style="display:none;"></iframe> 请登录');
 } 
//]]>
</script>
ltcszk 2014-02-18
  • 打赏
  • 举报
回复
var strUrl = "Customer/TimeOver?time="+Math.random; 你写的是相对地址啊 还有cookie的过期时间js可以直接取到没必要用轮询
Im_Sorry 2014-02-18
  • 打赏
  • 举报
回复
在前端不是一样么、??? 为什么要搞到后端? 前端一样可以根据时间来设置超时啊!
饕餮123 2014-02-17
  • 打赏
  • 举报
回复
信息不足无法判断,建议监听一下http响应
yezhubaobao 2014-02-17
  • 打赏
  • 举报
回复
引用 1 楼 gaowenzhen 的回复:
你这也太麻烦了吧,服务端session就有过期功能啊,难道我理解错了。
Session是在登陆的时候写入,然后超时的时候清空吗?
gaowenzhen 2014-02-14
  • 打赏
  • 举报
回复
你这也太麻烦了吧,服务端session就有过期功能啊,难道我理解错了。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧