方案一:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>定时跳转</title>
<style>
body{background:gray;}
div{margin:20px auto;width:350px;height:150px;border:1px solid #000;background:white;padding:10px;}
h2{text-align:center;}
span{font-size:150%;color:red;margin:0 10px;}
</style>
</head>
<body>
<div>
<h2>提交成功</h2>
<a href="http://www.example.com">
<span id="seconds">3</span>秒后系统会自动跳转,也可单击此链接跳转
</a>
</div>
<script>
function timing(secs, url) {
var seconds = document.getElementById('seconds');
seconds.innerHTML = secs--;
if (secs > 0) {
setTimeout('timing(' + secs + ', \'' + url + '\')', 1000);
} else {
location.href = url;
}
};
timing(8, 'http://www.example.com');
</script>
</body>
</html>
方案二:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>定时跳转</title>
<style>
body{background:gray;}
div{margin:20px auto;width:350px;height:150px;border:1px solid #000;background:white;padding:10px;}
h2{text-align:center;}
span{font-size:150%;color:red;margin:0 10px;}
</style>
</head>
<body>
<div>
<h2>提交成功</h2>
<a href="http://www.example.com">
<span id="seconds">5</span>秒后系统会自动跳转,也可单击此链接跳转
</a>
</div>
<script>
function timing(secs, url) {
var seconds = document.getElementById('seconds');
if (secs > 0) {
var timer = setInterval(all, 1000);
function all(){
seconds.innerHTML = --secs;
if(secs<=1){
clearInterval(timer);
location.href = url;
}
}
}
};
var seconds = document.getElementById('seconds');
timing(seconds.innerHTML, 'http://www.example.com');
</script>
</body>
</html>