1<html>
2<head>
3<script type="text/javascript">
4function show(obj)
5{ obj.style.display="none"
6setTimeout(show(obj),5000)
7}
8</script>
9</head>
10<body onload="show(d1)">
11<div display="" id="d1">
12<font color="red">▲</font>
13</div>
14</body>
15</html>
有兴趣的朋友可试试!
---------------------------------------------------------------
setTimeout("show(obj);",5000)
---------------------------------------------------------------
1<html>
2<head>
3<script type="text/javascript">
4function show(obj)
5{
6obj.style.display="none"
7setTimeout("show(d1)",5000)
8}
9</script>
10</head>
11<body onload="show(d1)">
12<div display="" id="d1">
13<font color="red">▲</font>
14</div>
15</body>
16</html>
---------------------------------------------------------------
llrock(百乐宝 ¦ ¦昨夜星辰) :
js解决setTimeout应该和队列和栈没有深关系。队列是fifo的,堆栈是lifo的,setTimeout是看你set到了什么时候的。
js中函数可视为对象,对函数的引用也可视为指针。
setTimeout("show(d1)",5000)是在5000毫秒后运行“show(d1)”这个语句(表达式)
setTimeout(show,5000)是setTimeout的第二种用法,因为show本身就是一个函数指针(引用),所以被安排在5000毫秒后调用它所指向的函数。
setTimeout(show(d1),5000)是马上运行这“show(d1)”这个表达式后尝试把结果当成函数指针来排队,如果show(d1)能够返回函数指针或者表达式就没问题,像这样(show返回函数):
function show(a){
return new Function("alert(""+a+"")");
}
setTimeout(show("hello"),1000);
或者这样(show返回表达式):
function show(a){
return ("alert(""+a+"")");
}
setTimeout(show("hello"),1000);
堆栈错误出在楼主在show函数里面的setTimeout语句又递归调用了show函数的执行结果,而不是show函数本身。
---------------------------------------------------------------
楼主将setTimeout("show(obj)",5000)
错用成setTimeout(show(obj),5000)
产生栈溢出,很正常啊
加了两句注释后
可以很清楚看清溢出的过程:
1<html>
2<head>
3<script type="text/javascript">
4var i=0;
5function show(obj)
6{ obj.style.display="none";
7alert(i++);
8if(i>10) return "alert('OK')";
9setTimeout(show(obj),5000)
10return "alert('"+i+"')";
11}
12</script>
13</head>
14<body onload="alert(show(d1))">
15<div display="" id="d1">
16<font color="red">▲</font>
17</div>
18</body>
19</html>