<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);
}这段似乎永远执行不到,大家看看这是咋回事呢?什么时候才会执行该条件呢?

解决方案 »

  1.   

    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").offsetHeight>=500){
    alert("500");
    clearTimeout(t);
    }
    }
    //主调函数
    function buttonJ(){
     if(document.getElementById("do").value == "收拢"){
      shoulong();
      document.getElementById("do").value = "展开";
     }else{
      expand();
      document.getElementById("do").value = "收拢";
     }  
    }
      

  2.   

    你部是写了判断吗?看执行alert没?还有把时间改大点吧,你1毫秒,你感觉得到?1000是一秒。
      

  3.   

    height+=20;if(document.getElementById("duilian").height==500)你每次加的是20,
    有可能加到490 的时候,再加20就到510了,永远不等于500
      

  4.   

    //展开
    function expand(){
    var obj =document.getElementById("duilian") ;
    var h = parseInt(obj.style.height) + 20;
    if(h<500){
    obj.style.height =  h+'px';
    setTimeout(expand,1);
    }else{
    obj.style.height =  '500px';
    }
    }其它函数 楼主照着改
      

  5.   


    恩,谢谢你,其实发完贴,自己回去看了一下就发现了,改成>500就OK 了!
      

  6.   


    恩,谢谢你,其实发完贴,自己回去看了一下就发现了,改成>500就OK 了!