Javascript / JQuery 随机图像重复问题
我有一个图像库,它从文件夹 images/flip_images/ 中提取随机图像。文件的名称为数字,然后是 .jpg,例如 0.jpg、1.jpg 等。有 14 个图像可供选择,标记为 0 - 13。因为只有 14 个图像,而页面一次需要 4 个图像,有相当多的重复,我想避免。我使用 Javascript 生成随机图像文件名并将变量传递到 JQuery .flip() 命令中。
我尝试在下面的代码中使用 do while 循环来更改全局变量,将全局变量与其他变量进行比较,并选择另一个数字(如果它首先选择的数字已在使用中)。然而,我似乎无法让它发挥作用。这是范围问题还是我只是在代码中犯了一个简单的错误?这是代码:
var randomNumber0= 0;
var randomNumber1= 1;
var randomNumber2= 2;
var randomNumber3= 3;
var refreshId0 = setInterval(function(){
var $this = $(this);
do {
randomNumber0=Math.floor(Math.random()*14)
}while(randomNumber0 == randomNumber1 || randomNumber0 == randomNumber2 || randomNumber0 == randomNumber3){
randomNumber0=Math.floor(Math.random()*14)
};
$("#flipboxa").flip({
direction: 'lr',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber0+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 5000);
var refreshId1 = setInterval(function(){
var $this = $(this);
do {
randomNumber1=Math.floor(Math.random()*14)
}while(randomNumber1 == randomNumber0 || randomNumber1 == randomNumber2 || randomNumber1 == randomNumber3){
randomNumber1=Math.floor(Math.random()*14)
};
$("#flipboxb").flip({
direction: 'rl',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber1+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 6000);
var refreshId2 = setInterval(function(){
var $this = $(this);
do {
randomNumber2=Math.floor(Math.random()*14)
}while(randomNumber2 == randomNumber0 || randomNumber2 == randomNumber1 || randomNumber2 == randomNumber3){
randomNumber2=Math.floor(Math.random()*14)
};
$("#flipboxc").flip({
direction: 'lr',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber2+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 7000);
var refreshId3 = setInterval(function(){
var $this = $(this);
do {
randomNumber3=Math.floor(Math.random()*14)
}while(randomNumber3 == randomNumber0 || randomNumber3 == randomNumber1 || randomNumber3 == randomNumber2){
randomNumber3=Math.floor(Math.random()*14)
};
$("#flipboxd").flip({
direction: 'rl',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber3+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 8000);
I have an image gallery which pulls in random images from the folder images/flip_images/. The files are names as a number then .jpg, e.g. 0.jpg, 1.jpg etc. There are 14 images which it can choose from, labelled 0 - 13. As there are only 14 images and the page needs 4 at a time, there is quite a bit of repeating, which I would like to avoid. I am using Javascript to generate the random image filenames and passing the variables into the JQuery .flip() command.
I have tried using the do while loop in the code below to change a global variable, compare the global variable to the others and pick another number if the number it chose in the first place is already in use. I can't, however, seem to get this to work. Is this a scope issue or have I just made a simple mistake in the code? Here is the code:
var randomNumber0= 0;
var randomNumber1= 1;
var randomNumber2= 2;
var randomNumber3= 3;
var refreshId0 = setInterval(function(){
var $this = $(this);
do {
randomNumber0=Math.floor(Math.random()*14)
}while(randomNumber0 == randomNumber1 || randomNumber0 == randomNumber2 || randomNumber0 == randomNumber3){
randomNumber0=Math.floor(Math.random()*14)
};
$("#flipboxa").flip({
direction: 'lr',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber0+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 5000);
var refreshId1 = setInterval(function(){
var $this = $(this);
do {
randomNumber1=Math.floor(Math.random()*14)
}while(randomNumber1 == randomNumber0 || randomNumber1 == randomNumber2 || randomNumber1 == randomNumber3){
randomNumber1=Math.floor(Math.random()*14)
};
$("#flipboxb").flip({
direction: 'rl',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber1+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 6000);
var refreshId2 = setInterval(function(){
var $this = $(this);
do {
randomNumber2=Math.floor(Math.random()*14)
}while(randomNumber2 == randomNumber0 || randomNumber2 == randomNumber1 || randomNumber2 == randomNumber3){
randomNumber2=Math.floor(Math.random()*14)
};
$("#flipboxc").flip({
direction: 'lr',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber2+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 7000);
var refreshId3 = setInterval(function(){
var $this = $(this);
do {
randomNumber3=Math.floor(Math.random()*14)
}while(randomNumber3 == randomNumber0 || randomNumber3 == randomNumber1 || randomNumber3 == randomNumber2){
randomNumber3=Math.floor(Math.random()*14)
};
$("#flipboxd").flip({
direction: 'rl',
color: "transparent",
content: '<img src="images/flip_images/'+randomNumber3+'.jpg" width="120" height="275" alt="alt tag here" />',
});
return false;
}, 8000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是
do..while
的正确形式试试这样:
This is not the proper form of a
do..while
Try it like this: