题目来自慕课网的javascript章节练习~
直接将script嵌入<body>中
setTimeout()实现读秒,window.location.href="",实现到期跳转。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=gkb"/> 5 <title>浏览器对象</title> 6 </head> 7 <body> 8 <p><font size=8>操作成功</font></p> 9 <p><b id="timeCount">5</b>秒后回到主页<a href="http://www.imooc.com"()>返回</a></p> 10 11 <script type="text/javascript"> 12 13 var countTime = 5; 14 function func(){ 15 document.getElementById("timeCount").innerHTML=countTime; 16 countTime--; 17 if(countTime==0){ 18 window.location.href="http://www.imooc.com"; 19 } 20 window.setTimeout("func()",1000); 21 22 } 23 func(); 24 </script> 25 </body> 26 </html>
如图: