<script type="text/javascript">
function second_count(){
var num=document.getElementById("id").innerHtml;
num=num-1;
if(num>=0){
setTimeout("second_count()",1000);
document.getElementById("id").innerHtml=num;
}else {
window.open("http://www.imooc.com")
}
}
second_count();
function backFn(){
window.history.back();
}
</script>
<!DOCTYPE html> <html> <head> <title>浏览器对象</title> <meta http-equiv="Content-Type" content="text/html; charset=gkb"/> </head> <body> <!--先编写好网页布局--> <h2>操作成功</h2> <p><span id="id">5</span>秒后回到主页<a href="" onclick="backFn()">返回</a></p> <script type="text/javascript"> var num =document.getElementById("id").innerHTML; function second_count(){ num=num-1; document.getElementById("id").innerHTML=num; if(num==0){ } } setInterval("second_count()",1000); </script> </body> </html>
修改了一下
1.setInterval("second_count()",1000);要放在函数外,放在函数内就无法调用自身
2.innerHTML后面的HTML是大写的