<body onload="timerb()"> <h3>操作成功</h3> <p id="p1"></p> <script> var num = 5; var timer = null; function timerb() { document.getElementById('p1').innerHTML = num + '秒后跳到首页' num--; if (!timer) { timer = setInterval("timerb()", 1000) } if (num === 0) { timer = null; location.replace('http://www.baidu.com') } } </script> </body>
方法写了没有触发,在最后调用下timeb就可以了
var num = 5;
var timer = null;
function timerb() {
document.getElementById('p1').innerHTML = num + '秒后跳到首页';
num--;
if (!timer) {
timer = setInterval("timerb()", 1000)
}
if (num === 0) {
timer = null;
location.replace('http://www.baidu.com')
}
}
timerb();
首先比较num等于0写错了