图库图像不会更改

时间:2015-06-27 16:42:33

标签: javascript jquery html

  

我已经实现了代码来检查服务器中是否存在图像文件的数量,然后将其用于图库。找到的图像数量正确,但只有第一张图像加载且不会改变。

var gallerylength;
var galleryid = 1;
function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}
function checksrc()
{
    var index = 1;
    var src = "images/splash/gallery/img";
    for(index=1;UrlExists(src+index+".jpg");index++);
    gallerylength = index;
}
function setimg()
{
     var src = "images/splash/gallery/img";
         {
            $("#gallerywindow").attr("src",src+galleryid+".jpg");
            if(galleryid<gallerylength-1)
                galleryid++;
            else
                galleryid = 1;          
        }
}
$(document).ready(function(event)
{
    checksrc();
    setInterval(setimg(),1000);
});

1 个答案:

答案 0 :(得分:1)

在行中:setInterval(setimg(),1000);您正在调用该函数,必须是对它的引用:setInterval(setimg,1000);

$(document).ready(function(event)
{
    checksrc();

    setInterval(setimg, 1000);

    //or

    setInterval(function(){
        setimg();
    }, 1000);
});