1. 程式人生 > >定時器之頁面跳轉

定時器之頁面跳轉

定時器之頁面跳轉

定時器:

只跳轉一次:

var t1 = setTimeout(function a(){},time);
cleanTimeout(t1);

迴圈跳轉多次:

var t2 = setInterval(function a(){},time);
clearInterval(t2);

js跳轉:

window.location.href='${pageContext.request.contextPath}/login/first';

專案路徑:

//==>localhost:8080/SSMProject
${pageContext.request.contextPath}

例子:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/12/26 0026
  Time: 下午 3:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>首頁</title>
<style>
h1 {
	text-align: center;
}
h4 {
	text-align: center;
	height: 100%;
	width: 100%;
}
</style>

</head>
<body>
	<h1>歡迎來到會員管理系統</h1>
	<h4 id="mes"></h4>
	<script type="text/javascript">
	var s = 6;
//  向頁面新增內容
	function f1(s){
		document.getElementById("mes").innerHTML="請稍等,還有"+s+"秒跳轉";
	}
	setInterval(function name(){
		s=s-1;
		f1(s);
		if(s==0){
			clearInterval();
			window.location.href='${pageContext.request.contextPath}/login/first';
		}
	}, 1000);
</script>
</body>
</html>