We often see that some web pages will prompt to automatically jump after a few seconds. Let's take a look at how this is achieved. We are learning. If the writing is not good, please advise me.
First of all, we need to use a setInterval timer, setInterval (code block to be executed, time: milliseconds); execute it once at any time and repeat it many times .
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;padding: 0;}
.box{
width: 400px;
height: 20px;
font-size: 20px;
color: red;
}
</style>
</head>
<body>
<div class="box">
<!-- 自己可以在span中定义自己想要多少秒之后进行跳转 -->
<span id="clock">5</span>秒后自动跳转到:百度
</div>
<script type="text/javascript">
var clock= document.getElementById("clock");
var n = clock.innerHTML;
function func(){
n--;
clock.innerHTML=n;
if (n<=0) {
// clearInterval(timer);
location.href = "https://www.baidu.com/"; //5秒后要跳转的地方
}
}
var timer = setInterval(func,1000);
</script>
</body>
</html>