实现步骤
显示文字
<p>
<span id="time">5span>秒后跳转首页
p>
定时器,让时间减少
var time = document.getElementById("time");
var s=5;
function changeTime(){
s--;
}
setInterval(changeTime,1000);
增加判断倒计时结束的跳转
if(s==0){
location.href="https://www.baidu.com";
}
else {
time.innerHTML = s;
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
p{
text-align: center;
}
span{
color: red;
}
style>
head>
<body>
<p>
<span id="time">5span>秒后跳转首页
p>
<script>
var time = document.getElementById("time");
var s=5;
function changeTime(){
s--;
if(s==0){
location.href="https://www.baidu.com";
}
else {
time.innerHTML = s;
}
}
setInterval(changeTime,1000);
script>
body>
html>