I have implemented the code to check if the number of image files present in the server and then use it for gallery. The number of images are found correct but only the first image loads and does not change .
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); });
最佳答案
行内:setInterval(setimg(),1000);
你正在调用函数,必须是对它的引用:setInterval(setimg,1000);
$(document).ready(function(event)
{
checksrc();
setInterval(setimg, 1000);
//or
setInterval(function(){
setimg();
}, 1000);
});
关于javascript - 图库图片没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091054/