这是一个无效的简单旋转脚本。它有四个警报:currentImage,1,2和3.在FF中它会经历四个警报并停止。在Chrome中,它经历了五次。图像仅在两个浏览器中更改一次。
function rotateImages(currentImage, id) {
var dir = "/images/";
var a = new Array("coolspider1.jpg", "coolspider2.jpg", "coolspider3.jpg");
var b = document.getElementById(id);
if(currentImage >= a.length){
currentImage=0;}
//loop stops here in ff
alert(a[currentImage]);
//loop stops here in chrome
b.src = dir + a[currentImage];
alert(1);
currentImage++;
alert(2);
rotator = window.setTimeout("rotateImages(" + currentImage + "," + id + ")",500);
alert(3);
}
答案 0 :(得分:1)
这里有很多问题:
var a = new Array(...)
可以创建一次。currentImage
for(i;etc..) setTimeout(func, 500 + i*500, params...)
替换:
window.setTimeout("rotateImages(" + currentImage + "," + id + ")",500);
使用:
window.setTimeout(rotateImages,500, currentImage, id);
setTimeout获取...... well,参数的可选参数。