对setTimeout的一点理解: MSDN中说: In versions earlier than Microsoft® Internet Explorer 5, the first argument of setTimeout must be a string. Evaluation of the string is deferred until the specified interval elapses. As of Internet Explorer 5, the first argument of setTimeout can be a string or a function pointer.IE5以上,第一个参数可以是string或者是函数指针. 所以上面的程序改为这样就可以了: <html> <head> <script>function changeImg() { var p1=Math.ceil(Math.random()*10); //alert(p1); document.getElementById("idImg1").src="img/"+p1+".jpg"; setTimeout("changeImg()",4000); } </script> </head> <body> <table><img id="idImg1" height=180 width=240></table> <script> changeImg(); </script> </body></html>
改为
setTimeout(changeImg(),4*1000);//每4秒钟执行一次
改为setTimeout(changeImg(),4*1000);也是一样显示内存不够
MSDN中说:
In versions earlier than Microsoft® Internet Explorer 5, the first argument of setTimeout must be a string. Evaluation of the string is deferred until the specified interval elapses.
As of Internet Explorer 5, the first argument of setTimeout can be a string or a function pointer.IE5以上,第一个参数可以是string或者是函数指针.
所以上面的程序改为这样就可以了:
<html>
<head>
<script>function changeImg()
{
var p1=Math.ceil(Math.random()*10);
//alert(p1);
document.getElementById("idImg1").src="img/"+p1+".jpg";
setTimeout("changeImg()",4000);
}
</script>
</head>
<body>
<table><img id="idImg1" height=180 width=240></table>
<script>
changeImg();
</script>
</body></html>
<html>
<head>
<script>function changeImg()
{
var p1=Math.ceil(Math.random()*10);
//alert(p1);
document.getElementById("idImg1").src="img/"+p1+".jpg";
setTimeout(changeImg,4000);//这里改为函数指针.
}
</script>
</head>
<body>
<table><img id="idImg1" height=180 width=240></table>
<script>
changeImg();
</script>
</body></html>
不知我这样理解有没有错呢。请高人指点...
img1[0]="img/"+1+".jpg";
img1[1]="img/"+2+".jpg";
img1[2]="img/"+3+".jpg";
img1[3]="img/"+4+".jpg";
img1[4]="img/"+5+".jpg";
img1[5]="img/"+6+".jpg";
img1[6]="img/"+7+".jpg";
img1[7]="img/"+8+".jpg";
img1[8]="img/"+9+".jpg";
img1[9]="img/"+10+".jpg";function changeImg()
{
var p1=Math.ceil(Math.random()*10);
idImg1.src=img1[p1];
setTimeout(changeImg(),4);
}
你上面的
setTimeout(changeImg(),4);是错了。
应该写成setTimeout('changeImg()',4000)不然会报错“内存不足”,因为setTimeout(changeImg(),4)这句首先会立即调用changeImg(),而不是过4 时间才调用.
还有,你用数组去代替的只是图像路径的字符串组,并不是load 图像.
{
changeImg()
}
就算chrome都執行不到