Javascript / JQuery 随机图像重复问题

发布于 2024-12-20 19:41:55 字数 2665 浏览 0 评论 0原文

我有一个图像库,它从文件夹 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-12-27 19:41:55

不是 do..while 的正确形式

do {

    //something

}while(...){

    //something else

};  

试试这样:

do {
    //something
}
while(...);

This is not the proper form of a do..while

do {

    //something

}while(...){

    //something else

};  

Try it like this:

do {
    //something
}
while(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文