0

下面的代码是在我的网站上工作的唯一代码(使用在线模板构建)所有其他 javascript 都崩溃了。(我在网络编程方面非常新手)我可以对此代码进行简单的更改,以便在显示所有图片之前没有重复图片?

var delay=7000 //set delay in miliseconds
var curindex=0

var randomimages=new Array()
randomimages[0]="content/indsidebar1(2).jpg"
randomimages[1]="content/indsidebar2.jpg"
randomimages[2]="content/indsidebar3(3).jpg"
randomimages[3]="content/indsidebar6a.jpg"
randomimages[4]="content/indsidebar5.jpg"
randomimages[5]="content/indsidebar6.jpg"
randomimages[6]="content/indsidebar7.jpg"
randomimages[7]="content/indsidebar8.jpg"
randomimages[8]="content/indsidebar9.jpg"
randomimages[9]="content/indsidebar10.JPG"
randomimages[10]="content/commersidebar1.JPG"
randomimages[11]="content/commersidebar2.JPG"
randomimages[12]="content/commersidebar4.JPG"
randomimages[13]="content/commersidebar5.JPG"
randomimages[14]="content/commersidebar6.JPG"
randomimages[15]="content/commersidebar7.JPG"
randomimages[16]="content/ressidebar1.JPG"
randomimages[17]="content/ressidebar2.JPG"
randomimages[18]="content/ressidebar3.JPG"
randomimages[19]="content/ressidebar4.JPG"
randomimages[20]="content/communsidebar1.JPG"
randomimages[21]="content/communsidebar3.JPG"
randomimages[22]="content/communsidebar4.JPG"
randomimages[23]="content/communsidebar5.JPG"
randomimages[24]="content/communsidebar6.JPG"
randomimages[25]="content/communsidebar7.JPG"
randomimages[26]="content/communsidebar8.JPG"
randomimages[27]="content/communsidebar10.JPG"
randomimages[28]="content/undersidebar1.jpg"
randomimages[29]="content/undersidebar2(1).jpg"
randomimages[30]="content/undersidebar3.jpg"
randomimages[31]="content/undersidebar4.jpg"
randomimages[32]="content/servicesidebar1.jpg"
randomimages[33]="content/servicesidebar2.jpg"
randomimages[34]="content/servicesidebar6.jpg"
randomimages[35]="content/servicesidebar4.JPG"
randomimages[36]="content/servicesidebar5.JPG"

var preload=new Array()

for (n=0;n<randomimages.length;n++)
{
    preload[n]=new Image()
    preload[n].src=randomimages[n]
}

document.write('<img name="defaultimage" src="'+
  randomimages[Math.floor(Math.random()*(randomimages.length))]+'">')

function rotateimage()
{
    if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length))))
        curindex=curindex==0? 1 : curindex-1
    else
        curindex=tempindex

    document.images.defaultimage.src=randomimages[curindex]
}

setInterval("rotateimage()",delay)
4

5 回答 5

1

我同意Derek Henderson更好编码的建议。

可能您可以使用以下逻辑:

  1. 将 randomimages 复制到 original_randomimages 数组。
  2. 从数组 randomimages(例如第 i 个元素)中选择并显示随机图片。
  3. 使用拼接方法( https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice)从 randomimages 数组中删除显示的图片。
  4. 重复第 2 步和第 3 步,直到 randomimages 数组为空。
  5. 将 original_randomimages 复制到 randomimages。
  6. 从第 2 步开始再次重复。
于 2013-05-17T17:55:52.377 回答
0

如果您想在随机生成图像后重复随机化,您可以编写一个类似的函数,然后在退出 while 循环后再次调用该函数。

var delay=7000 //set delay in miliseconds
var curindex=0

var randomimages=new Array()
randomimages[0]="content/indsidebar1(2).jpg"
randomimages[1]="content/indsidebar2.jpg"
randomimages[2]="content/indsidebar3(3).jpg"
randomimages[3]="content/indsidebar6a.jpg"
randomimages[4]="content/indsidebar5.jpg"
randomimages[5]="content/indsidebar6.jpg"
randomimages[6]="content/indsidebar7.jpg"
randomimages[7]="content/indsidebar8.jpg"
randomimages[8]="content/indsidebar9.jpg"
randomimages[9]="content/indsidebar10.JPG"
randomimages[10]="content/commersidebar1.JPG"
randomimages[11]="content/commersidebar2.JPG"
randomimages[12]="content/commersidebar4.JPG"
randomimages[13]="content/commersidebar5.JPG"
randomimages[14]="content/commersidebar6.JPG"
randomimages[15]="content/commersidebar7.JPG"
randomimages[16]="content/ressidebar1.JPG"
randomimages[17]="content/ressidebar2.JPG"
randomimages[18]="content/ressidebar3.JPG"
randomimages[19]="content/ressidebar4.JPG"
randomimages[20]="content/communsidebar1.JPG"
randomimages[21]="content/communsidebar3.JPG"
randomimages[22]="content/communsidebar4.JPG"
randomimages[23]="content/communsidebar5.JPG"
randomimages[24]="content/communsidebar6.JPG"
randomimages[25]="content/communsidebar7.JPG"
randomimages[26]="content/communsidebar8.JPG"
randomimages[27]="content/communsidebar10.JPG"
randomimages[28]="content/undersidebar1.jpg"
randomimages[29]="content/undersidebar2(1).jpg"
randomimages[30]="content/undersidebar3.jpg"
randomimages[31]="content/undersidebar4.jpg"
randomimages[32]="content/servicesidebar1.jpg"
randomimages[33]="content/servicesidebar2.jpg"
randomimages[34]="content/servicesidebar6.jpg"
randomimages[35]="content/servicesidebar4.JPG"
randomimages[36]="content/servicesidebar5.JPG"

var preload=new Array()

for (n=0;n<randomimages.length;n++)
{
    preload[n]=new Image()
    preload[n].src=randomimages[n]
}


var checkArray = [];
var length = randomimages.length;

function generateImages () {

    var i = generateRandomValue();
    if (checkArray.length <= length){
        if(checkArray.indexOf(i) === -1) {
          document.write('<img name="defaultimage" src="'+randomimages[i]+'">')
          checkArray.push(i);
       } else {
          generateImages();      
       }
    } else if (checkArray.length === length) {
        checkArray = [];
        generateImages();
    }

 }

function  generateRandomValue () {

 return Math.floor(Math.random()*(randomimages.length));

}

function rotateimage()
{
    if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length))))
        curindex=curindex==0? 1 : curindex-1
    else
        curindex=tempindex

    document.images.defaultimage.src=randomimages[curindex]
}

generateImages();
setInterval("rotateimage()", delay);
于 2013-05-17T17:49:29.563 回答
0

我稍微更改了您的代码,但没有测试它,所以请检查它,但它应该可以工作。

<script language="javascript">

var delay=7000; //set delay in miliseconds
var curindex=0;

var randomimages= [];

    randomimages[0]="content/indsidebar1(2).jpg";
    randomimages[1]="content/indsidebar2.jpg";
    randomimages[2]="content/indsidebar3(3).jpg";
    randomimages[3]="content/indsidebar6a.jpg";
    randomimages[4]="content/indsidebar5.jpg";
    randomimages[5]="content/indsidebar6.jpg";
    randomimages[6]="content/indsidebar7.jpg";
    randomimages[7]="content/indsidebar8.jpg";
    randomimages[8]="content/indsidebar9.jpg";
    randomimages[9]="content/indsidebar10.JPG";


    randomimages[10]="content/commersidebar1.JPG";
    randomimages[11]="content/commersidebar2.JPG";
    randomimages[12]="content/commersidebar4.JPG";
    randomimages[13]="content/commersidebar5.JPG";
    randomimages[14]="content/commersidebar6.JPG";
    randomimages[15]="content/commersidebar7.JPG";


    randomimages[16]="content/ressidebar1.JPG";
    randomimages[17]="content/ressidebar2.JPG";
    randomimages[18]="content/ressidebar3.JPG";
    randomimages[19]="content/ressidebar4.JPG";


    randomimages[20]="content/communsidebar1.JPG";
    randomimages[21]="content/communsidebar3.JPG";
    randomimages[22]="content/communsidebar4.JPG";
    randomimages[23]="content/communsidebar5.JPG";
    randomimages[24]="content/communsidebar6.JPG";
    randomimages[25]="content/communsidebar7.JPG";
    randomimages[26]="content/communsidebar8.JPG";
    randomimages[27]="content/communsidebar10.JPG";




    randomimages[28]="content/undersidebar1.jpg";
    randomimages[29]="content/undersidebar2(1).jpg";
    randomimages[30]="content/undersidebar3.jpg";
    randomimages[31]="content/undersidebar4.jpg";



    randomimages[32]="content/servicesidebar1.jpg";
    randomimages[33]="content/servicesidebar2.jpg";
    randomimages[34]="content/servicesidebar6.jpg";
    randomimages[35]="content/servicesidebar4.JPG";
    randomimages[36]="content/servicesidebar5.JPG";


//You are not using preload anywhere so it is not necessary

var irand = Math.floor(Math.random()*(randomimages.length));

document.write('<img name="defaultimage" src="'+randomimages[irand]+'">');

var shown = [];
shown[irand] = true;

function rotateimage()
{
    var itemp = Math.floor(Math.random()*(randomimages.length));
    if(shown[itemp])
       return;
    document.images.defaultimage.src=randomimages[itemp];
    shown[itemp] = true;
    if (shown.length == 36)
        shown = [];
}

setInterval(rotateimage, delay);

    </script>

更新: 更正版本:

<script language="javascript">

    var delay=7000; //set delay in miliseconds
    var curindex=0;

    var randomimages= [];

        randomimages[0]="content/indsidebar1(2).jpg";
        randomimages[1]="content/indsidebar2.jpg";
        randomimages[2]="content/indsidebar3(3).jpg";
        randomimages[3]="content/indsidebar6a.jpg";
        randomimages[4]="content/indsidebar5.jpg";
        randomimages[5]="content/indsidebar6.jpg";
        randomimages[6]="content/indsidebar7.jpg";
        randomimages[7]="content/indsidebar8.jpg";
        randomimages[8]="content/indsidebar9.jpg";
        randomimages[9]="content/indsidebar10.JPG";


        randomimages[10]="content/commersidebar1.JPG";
        randomimages[11]="content/commersidebar2.JPG";
        randomimages[12]="content/commersidebar4.JPG";
        randomimages[13]="content/commersidebar5.JPG";
        randomimages[14]="content/commersidebar6.JPG";
        randomimages[15]="content/commersidebar7.JPG";


        randomimages[16]="content/ressidebar1.JPG";
        randomimages[17]="content/ressidebar2.JPG";
        randomimages[18]="content/ressidebar3.JPG";
        randomimages[19]="content/ressidebar4.JPG";


        randomimages[20]="content/communsidebar1.JPG";
        randomimages[21]="content/communsidebar3.JPG";
        randomimages[22]="content/communsidebar4.JPG";
        randomimages[23]="content/communsidebar5.JPG";
        randomimages[24]="content/communsidebar6.JPG";
        randomimages[25]="content/communsidebar7.JPG";
        randomimages[26]="content/communsidebar8.JPG";
        randomimages[27]="content/communsidebar10.JPG";




        randomimages[28]="content/undersidebar1.jpg";
        randomimages[29]="content/undersidebar2(1).jpg";
        randomimages[30]="content/undersidebar3.jpg";
        randomimages[31]="content/undersidebar4.jpg";



        randomimages[32]="content/servicesidebar1.jpg";
        randomimages[33]="content/servicesidebar2.jpg";
        randomimages[34]="content/servicesidebar6.jpg";
        randomimages[35]="content/servicesidebar4.JPG";
        randomimages[36]="content/servicesidebar5.JPG";


    //You are not using preload anywhere so it is not necessary

    var irand = Math.floor(Math.random()*(randomimages.length));

    document.write('<img name="defaultimage" src="'+randomimages[irand]+'">');

    var shown = [];
    shown[irand] = true;

    function rotateimage()
    {
        var itemp = Math.floor(Math.random()*(randomimages.length));
        if(shown[itemp])
           return;
        document.images.defaultimage.src=randomimages[itemp];
        shown[itemp] = true;
        if (shown.length == 36)
            shown.pop();
    }

    setInterval(rotateimage, delay);

        </script>
于 2013-05-17T17:57:12.527 回答
0

正如我在评论中所说,我建议使用数组的随机播放(Fisher-Yates 是我对此类事情的首选算法)。类似于:

function ShuffleImages(){
    for(var i = randomimages.length - 1; i > 0; i--){
        var j = Math.floor(Math.random() * i);
        var _tmp = randomimages[i];
        randomimages[i] = randomimages[j];
        randomimages[j] = _tmp;
    }
}

这在创建随机化时不会导致冲突,并确保没有重复的图像。然后,您可以像往常一样遍历数组,并在需要时再次随机播放。

于 2013-05-17T18:02:42.960 回答
0

只需从您的来源中删除已显示的图像

<script language="javascript">
var delay = 7000; //set delay in miliseconds
var curindex = 0;

var randomimages = [
    "content/indsidebar1(2).jpg",
    "content/indsidebar2.jpg",
    "content/indsidebar3(3).jpg",
    // add the rest of your images here without , at the end
];

// make a copy of your array as source
var src = randomimages.slice(0);

var preload = [];

for (var n = 0; n < randomimages.length; n++)
{
    preload[n] = new Image();
    preload[n].src = randomimages[n];
}

document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random() * randomimages.length)]+'">');

function rotateimage()
{
    if (curindex == (tempindex=Math.floor(Math.random()*(randomimages.length)))){
        curindex = curindex == 0 ? 1 : curindex - 1;
    } else {
        curindex = tempindex;
    }

    document.images.defaultimage.src = randomimages[curindex];

    // remove from source
    ramdomimages.splice(curindex, 1);

    // re-copy src when empty to start new run
    if (randomimages.length == 0) {
        randomimages = src.slice(0);
    }
}

setInterval(
    function() {
        rotateimage();
    }
    , delay
);
</script>
于 2013-05-17T18:03:22.607 回答