setTimeout()为啥停不下来?
泥蒿 2011-07-06 05:20:57 <html>
<head>
<title>SetTimeout</title>
<script type="text/javascript">
//该脚本用于控制页面中的对联展开和收拢
//收拢
function shoulong(){
document.getElementById("duilian").height-=20;
var t = setTimeout("shoulong()",1);
if(document.getElementById("duilian").height == 0){
document.getElementById("duilian").height = 10;
clearTimeout(t);
}
}
//展开
function expand(){
document.getElementById("duilian").height+=20;
var t = setTimeout("expand()",1);
if(document.getElementById("duilian").height==500){
alert("500");
clearTimeout(t);
}
}
//主调函数
function buttonJ(){
if(document.getElementById("do").value == "收拢"){
shoulong();
document.getElementById("do").value = "展开";
}else{
expand();
document.getElementById("do").value = "收拢";
}
}
</script>
</head>
<body>
<input type="button" length="20" value="收拢" onclick="buttonJ()" id="do"/>
<div width="200" height="500">
<img type="text" id="duilian" src="img/duilian.jpg" width="200" height="500"/>
</div>
</body>
</html>
上面的代码,expand()函数中
if(document.getElementById("duilian").height==500){
alert("500");
clearTimeout(t);
}
这段似乎永远执行不到,大家看看这是咋回事呢?什么时候才会执行该条件呢?