Asp.net利用Session实现倒计时
时间:2010-09-01 来源:码魔谷
Asp.net利用Session实现倒计时
前台aspx页面:
<script type="text/javascript">
function sessionTimeout()
{
var lblMinute=document.getElementById("lblMinute");
var lblTime=document.getElementById("lblTime");
var minute=parseInt(lblMinute.value);
var seconds=parseInt(lblTime.value);
if(seconds==0&&minute>0)
{
minute=minute-1;
seconds=59;
}
else
{
seconds=seconds-1;
}
lblMinute.value=minute.toString()+":";
lblTime.value=seconds.toString();
if(minute==0&&seconds==0)
{
alert("您已超时");
window.location.href="login.aspx";//转到登陆页
clearInterval(sessionTimeout);
}
}
window.setInterval(sessionTimeout,1000);
</script>
<div>剩余时间:
<input ID="lblMinute" name="lblMinute" runat="server" type="text" style="border:0px;width:20px;" />
<input ID="lblTime" name="lblTime" runat="server" type="text" style="border:0px;width:20px;" />
<asp:Button ID="Button1" runat="server" Text="操作" /></div>
cs文件:
protected void Page_Load(object sender, EventArgs e)
{
Session["time"] = System.DateTime.Now.ToString();
Session.Timeout = 1;
lblMinute.Value = Convert.ToString(Convert.ToInt32(Session.Timeout) * 30); //默认考试时间为30分钟
lblTime.Value = "0";
}