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);


这段似乎永远执行不到,大家看看这是咋回事呢?什么时候才会执行该条件呢?

...全文
145 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
泥蒿 2011-07-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wangcheng_714 的回复:]

这个根本就加不到500呀 楼主

只能是490或者510
[/Quote]

恩,谢谢你,其实发完贴,自己回去看了一下就发现了,改成>500就OK 了!
泥蒿 2011-07-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jiangsuwx 的回复:]

结贴率太低 lz
[/Quote]

恩,谢谢你,其实发完贴,自己回去看了一下就发现了,改成>500就OK 了!
wangcheng_714 2011-07-17
  • 打赏
  • 举报
回复
这个根本就加不到500呀 楼主

只能是490或者510
学学你们 2011-07-08
  • 打赏
  • 举报
回复
结贴率太低 lz
田想兵 2011-07-08
  • 打赏
  • 举报
回复
你这个递归不像递归的,不带这么弄的。
ZiSheng 2011-07-06
  • 打赏
  • 举报
回复
每次加20,490,然后是510,500很难过。
hch126163 2011-07-06
  • 打赏
  • 举报
回复
//展开
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';
}
}

其它函数 楼主照着改
lsw645645645 2011-07-06
  • 打赏
  • 举报
回复
height+=20;

if(document.getElementById("duilian").height==500)

你每次加的是20,
有可能加到490 的时候,再加20就到510了,永远不等于500
coxie007 2011-07-06
  • 打赏
  • 举报
回复
你部是写了判断吗?看执行alert没?还有把时间改大点吧,你1毫秒,你感觉得到?1000是一秒。
挨踢直男 2011-07-06
  • 打赏
  • 举报
回复
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 = "收拢";
}
}

87,919

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧