<script type="text/javascript"> var timer = window.setInterval("Timer()",1000);//启用定时倒计 var secs = 5; function Timer() { secs--; document.getElementById("btnShow").innerHTML = "倒计时:[" + secs + "]"; if(secs == 0){ window.clearInterval(timer);//清楚定时任务 window.location.href="地址"; } } </script>
实例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>X-Open</title> <link type="text/css" rel="stylesheet" href="static/css/bootstrap.min.css"> <script type="text/javascript" src="static/js/jquery.min.js"></script> <script type="text/javascript" src="static/js/bootstrap.min.js"></script> </head> <body style="margin: 0px;"> <%@ include file="/WEB-INF/jsps/header.jsp"%> <!-- 内容部分 --> <div class="container"> <div class="row"> <div class="col col-md-12" style="text-align: center;">你注册成功啦<br> 现在可以去<a href="<c:url value='/login.html'/>">登陆啦</a> <br> <label id="lable"></label> </div> </div> </div> </body> <!-- 倒计时脚本 --> <script type="text/javascript"> var timer = window.setInterval("Timer()",1000);//启用定时倒计 var secs = 5; function Timer() { secs--; document.getElementById("lable").innerHTML = secs+"秒以后自动跳转到登陆页面"; if(secs == 0){ window.clearInterval(timer);//清除定时任务 window.location.href="<c:url value='/login.html'/>"; } } </script> </html>