首页 > 代码库 > setTimeout递归调用跳转页面

setTimeout递归调用跳转页面

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <script>
 7         window.onload = function () {
 8             function $(id){
 9                 return document.getElementById(id);
10             }
11             setTimeout(goIndexPage,1000);
12             var count = 5;
13             function goIndexPage(){
14                 count--;
15                 $("demo").innerHTML = "<a href=http://www.mamicode.com/‘http://www.baidu.com‘>还有"+count+"秒即可跳转</a>";
16 
17                 if(count<=0){
18 
19                     window.location.href = "http://www.baidu.com";
20                 }
21                 else {
22                     setTimeout(goIndexPage,1000);
23                 }
24             }
25 
26         }
27     </script>
28 </head>
29 <body>
30     <div id="demo"></div>
31 </body>
32 </html>

 

setTimeout递归调用跳转页面