題目:實現用戶登陸后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>
新聞熱點
疑難解答