为什么加载后最先显示的是undefined秒,然后才开始5秒倒计时呢?
<!DOCTYPE html> <html> <head> <title>浏览器对象</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <p style="font-size: 24px;font-weight: bold;">操作成功</p> <p style="font-size: 20px"><span id="i">5</span>秒后返回 <a href="javascript:window.history.back();">返回</a></p> <script type="text/javascript"> clock(); var j=5; function clock(){ document.getElementById("i").innerHTML=j; j--; if(j==0){window.location.href="http://www.imooc.com/"} setTimeout(clock,1000); } //通过window的location和history对象来控制网页的跳转。 </script> </body> </html>
为什么加载后最先显示的是undefined秒,然后才开始5秒倒计时呢?明明我span标签内预先设置了5秒啊??