试图创建简单的JavaScript幻灯片放映

时间:2014-01-09 20:37:04

标签: javascript jquery

幻灯片将在六个图像之间旋转1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg

var showarray = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg"];

var i = 0;

for( i = 0; i < 6; i++) 
{
    // How can I make this script wait for two seconds in this spot
    // so the pic is displayed for two seconds? 

    document.getElementById("imageid").src = showarray[i];
    if(i == 5) i = 0;

}

如果这是另一种方式,我也会在此页面中包含jQuery。

提前多多感谢

2 个答案:

答案 0 :(得分:3)

查看setTimeout()方法。这样称呼:

setTimeout(function() {
   // change the source of the image here.
}, 1000 /* 1 SECOND */);

答案 1 :(得分:2)

你不想让它睡两秒钟。这将从浏览器中取消控制并且非常糟糕。试试这样的事情(我对JS不好):

var showarray = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg"];
var i = 0;
var newPic;

newPic = function() {
  i = i+1 % 6;
  document.getElementById("imageid").src = showarray[i];
  setTimeout(newPic, 2000);
}

newPic();