function call()
{
var i=0,
img=document.getElementById("img1");
for(i=1;i<=3;i=i++)
{
setInterval(function(){
img.src="Images/"+i+".jpg";
},3000);
}
}
网页上没有显示任何内容。图像名称是正确的。当代码执行时,计算机挂起。
最佳答案
i=i++
这不是递增i
的正确用法。 .
它的作用:
- 变量
i
增加++
. - 变量
i
被分配旧值i
,如i++
返回i
的旧值.
所以基本上,你陷入了无限循环。只需替换 i=i++
只需 i++
或i=i+1
或i+=1
你应该没问题。
再次查看您的代码,我必须告诉您,这仍然不会产生所需的效果,因为您将所有间隔设置为在同一时间触发。你想要做的事情看起来像这样:
function next() {
setInterval(function(){
//display next image
},3000);
next();
}
next();
根据您的 html 代码的构建方式,实现此目的的最简单方法是:
var count = 3; current = 0, img=document.getElementById("img1");
function next() {
setInterval(function(){
current++;
if(current >= count) {
current = 0;
}
img.src="Images/" + current + ".jpg";
},3000);
next();
}
next();
关于javascript - 我正在使用此代码在 javascript 中滑动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24605210/