Javascript 不要重复上次使用的图像
如何使出现的下一个图像不重复上次使用的图像Javascript 不要重复上次使用的图像,javascript,jquery,css,Javascript,Jquery,Css,如何使出现的下一个图像不重复上次使用的图像 var imgs = new Array("https://store.vtxfactory.org/wp-content/uploads/2018/header/1.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/2.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/3.jpg","ht
var imgs = new Array("https://store.vtxfactory.org/wp-content/uploads/2018/header/1.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/2.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/3.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/4.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/5.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/6.jpg");
function changeBg() {
var imgUrl = imgs[Math.floor(Math.random()*imgs.length)];
$('#masthead').css('background-image', 'url(' + imgUrl + ')');
};
setInterval(changeBg,10000);
存储上次使用的索引,并重新生成与之匹配的随机数:
var lastIndex = -1;
function changeBg() {
var index;
do {
index = Math.floor(Math.random()*imgs.length);
} while (index == lastIndex);
var imgUrl = imgs[index];
lastIndex = index;
$('#masthead').css('background-image', 'url(' + imgUrl + ')');
};
您可以存储最后使用的一个,并生成新的,直到您想要的成为现实
var imgs=新数组(“https://store.vtxfactory.org/wp-content/uploads/2018/header/1.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/2.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/3.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/4.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/5.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/6.jpg");
var imgUrl=imgs[Math.floor(Math.random()*imgs.length)];
var lastUsed=“”;
函数changeBg(){
while(imgUrl!==上次使用)
imgUrl=imgs[Math.floor(Math.random()*imgs.length)];
$('#masthead').css('background-image','url('+imgUrl+'));
lastUsed=imgUrl;
};
setInterval(changeBg,10000);
与其从数组中选择一个随机项并检查它是否发生,我建议您洗牌数组(以获得随机选择)然后将第一个项目移出。这将允许对项目进行随机和唯一选择,并从可用阵列中删除当前项目。要允许补充原始阵列,将每个项目推入usedImages阵列,然后在其为空时将使用过的图像重新运行到原始阵列
请注意,这将防止复制相同的项目,但也会改变原始阵列(尽管它会在清空阵列时进行补充)。
由于我没有访问后台图像的权限-我是控制台。记录url以证明它始终是唯一的,并且阵列已被补充。出于演示目的,我还将间隔缩短为3秒
var-imgs=[”https://store.vtxfactory.org/wp-content/uploads/2018/header/1.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/2.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/3.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/4.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/5.jpg","https://store.vtxfactory.org/wp-content/uploads/2018/header/6.jpg"];
var usedImages=[];
changeBg();
函数changeBg(){
洗牌(imgs);
var imgUrl=imgs.shift();
console.log(imgUrl);
$('#masthead').css('background-image','url('+imgUrl+'));
usedImages.push(imgUrl)
如果(imgs.length==0){
imgs=使用图像;
usedImages=[]
};
};
函数洗牌(数组){
var currentIndex=array.length,temporaryValue,randomIndex;
而(0!==currentIndex){
randomIndex=Math.floor(Math.random()*currentIndex);
currentIndex-=1;
临时值=数组[currentIndex];
数组[currentIndex]=数组[randomIndex];
数组[randomIndex]=临时值;
}
返回数组;
}
设置间隔(changeBg,3000);