奇怪的循环终止(不会弹出窗口,只是innerHTML了)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
</head>
<body onload="sb();">
<script language="JavaScript">
<!--
var a = 0;
var x;
function sb() {
if (1 < 5)
{
a ++;
if (a > 10)
{
document.getElementById("over").innerHTML += "超时";
clearInterval(x);
xb();
}
else
{
x = setInterval("sb()", 10);
}
}
}
function xb(){
document.getElementById("div1").innerHTML = a;
}
//-->
</script>
<div id="over"></div>
<div id="div1"></div>
</body>
</html>
我想让a>10的时候清除定时器并且执行xb函数,为什么不能清除呢?....
------解决方案--------------------HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
</head>
<body onload="sb();">
<script language="JavaScript">
<!--
var a = 0;
var x=null;
function sb() {
a++;
if (a > 10)
{
document.getElementById("over").innerHTML += "超时";
clearInterval(x);
xb();
}
else if(x==null)//判断这个定时器是否已经存在
{
x = setInterval("sb()", 10);
}
}
function xb(){
document.getElementById("div1").innerHTML = a;
}
//-->
</script>
<div id="over"> </div>
<div id="div1"> </div>
</body>
</html>