28,406
社区成员
发帖
与我相关
我的任务
分享<FORM name=myform>剩余时间: <INPUT class=text2 size=10 value=剩余时间
name=clock> <INPUT type=hidden size=50 name=times>
<%
Session("tryTime")="2008/9/9 09:50:00" '这里必须是设定的一个时间,你可以是在登陆的时候设置
TimeGet=Session("tryTime")''这是ASP控制的时间
%>
<SCRIPT language=JavaScript>
var id, iM = 0, iS = 1;
var start1 ="<%=TimeGet%>";
var start=new Date(start1)
if (document.myform.times.value==""){
starts=start.getTime();
}else{
starts=document.myform.times.value;
}
//document.myform.clock.value=start.getSeconds();
function go()
{
now = new Date();
time = (2700000-(now.getTime()-starts))/1000;
//time = (10000-(now.getTime()-starts))/1000;
time = Math.floor( time);
iS = time % 60;
iM = Math.floor( time / 60);
if ( iS < 10){
document.myform.clock.value = " " + iM + ":0" + iS;
}else{
document.myform.clock.value = " " + iM + ":" + iS;
}
document.myform.times.value =starts;
if (time==300){
alert("请抓紧时间答题,距交卷还有5分钟。");
}
if (time==0){
alert("时间到,请交卷。");
window.location="test.asp?gopage=jiaojuan&choose=" + document.form1.choose.value;
}
id = setTimeout( "go()", 1000);
}
go();
</SCRIPT>
</form>
题目:实现用户登陆后15分钟之后强制退出。
思路:
用JS写一个当前时间与登陆时间对比的方法。当两时间分钟值相减大于15时,那就用location.href跳转到退出的ASP页面。
疑问:怎样实现这JS方法的反复执行,以达到时间的对比?
解决办法:用JS的setTimeout函数可以解决这个疑问。
程序代码
<script language="JavaScript">
<!--
login_time=15;//设置允许登陆的时长,单位为分钟。
sm=<%=minute(session("in_time"))%>;//登陆时的分钟,session("in_time")为登陆时间。
sh=<%=hour(session("in_time"))%>;//登陆时的时钟,session("in_time")为登陆时间。
CheckTime();
function CheckTime(){//检查现在时间
nowtime= new Date();
nh=nowtime.getHours()
nm=nowtime.getMinutes()
if (nh > sh) nm +=60//当前时钟与登陆时的时钟做比较
//登陆时间超过login_time,退出
if ((snm - sm) > login_time){
alert("您登陆已经超过15分钟,系统将强制您退出!");
location.href("login_out.asp");
//parent.window.close();
}
delete nowtime;
setTimeout("CheckTime()","10000");//实现对CheckTime()方法的不停执行。10000为毫秒,1s=1000毫秒
}
-->
</script>