这在e上是一个doozey。

我有while循环来生成一个随机数,该随机数与之前产生的任何其他随机数都不相同。随机数用于从对象中选择文本值。

例如:

quoteArray[1] = "some text"
quoteArray[2] = "some different text"
quoteArray[3] = "text again"
quoteArray[4] = "completely different text"
quoteArray[5] = "ham sandwich"


这是较大函数的一部分,在该函数循环经过= quoteArray.length之后,它将重置并重新开始循环。我遇到的问题是以下代码有时会产生无限循环:

//Note: at this point in the function I have generated a random number once already and stored it in 'randomnumber'
//I use this while statement to evaluate 'randomnumber' until the condition of it NOT being a number that has already been used and NOT being the last number is met.
while(randomnumber === rotationArray[randomnumber] || randomnumber === lastnumber){
    randomnumber = Math.floor(Math.random() * (quoteArray.length));
}


当我console.log(randomnumber)时-当我陷入循环中时-结果就是“ 0”。当陷入循环中时,似乎Math.floor(Math.random()*(quoteArray.length))不会产生随机数,而是无限地只是“ 0”。

谁能告诉我为什么我遇到这个问题?


编辑:这是带有函数+变量声明的完整相关代码



//初始化quoteObj的函数
    函数quoteObj(text,cname,ccompany,url,height){
            this.text = text;
            this.cname = cname;
            this.ccompany = ccompany;
            this.url = url;
            this.height =高度;
        }

//用XML表单中的报价信息填充我的quotes对象。

    var qObj = new quoteObj('','','','');
    var quoteArray = new Array();
    var计数器= 0;

//循环遍历每个XML项并将数据加载到对象中,然后将其存储在数组中
    $ .ajax({
        类型:“ GET”,
        网址:“ quotes.xml”,
        dataType:“ xml”,
        成功:function(xml){
            $(xml).find('quote')。each(function(){
                quoteArray [counter] = new quoteObj('','','','');
                console.log(quoteArray [counter]);
                quoteArray [counter] .text = $(this).find('text')。text();
                quoteArray [counter] .cname = $(this).find('customer_name')。text();
                quoteArray [counter] .ccompany = $(this).find('customer_company')。text();
                quoteArray [counter] .url = $(this).find('project')。text();
                ++计数器;
            });
        }
    });

//这是产生我的无限循环问题的部分。
//如果人们想知道有关如何初始化项目的特定信息,我会包含所有其他代码。

//生成随机的第一引号,然后随机遍历整个集合并重新开始。

    var randomnumber = Math.floor(Math.random()*(quoteArray.length));
    var rotationArray = new Array(quoteArray.length);
    var v = 0;
    var lastnumber = -1;

    bHeight = $('#rightbox')。height()+ 50;
    var cHeight = 0;
    var divtoanim = $('#customerquotes')。parent();

//不相关的//
//给内阴影一个高度,以便隐藏的引号适用于引号。
    $(divtoanim).css({'height':bHeight});

//随机旋转报价功能。
    setInterval(function(){
        randomnumber = Math.floor(Math.random()*(quoteArray.length));

//检查功能循环是否需要从头开始。
        if(v ==(quoteArray.length)){
            rotationArray.length = 0;
            v = 0;
        }
//确定随机数是否与之前生成的任何其他随机数都不相同,并且与上一个随机数不同
        while(randomnumber === rotationArray [randomnumber] || randomnumber === lastnumber){
            randomnumber = Math.floor(Math.random()*(quoteArray.length));
        }

        lastnumber = randomnumber;
        rotationArray [randomnumber] =随机数;
        ++ v;

//不相关的//
//动画序列
        $('#ctext,#cname')。animate({'opacity':'0'},2000,function(){
            $('#ctext')。html(quoteArray [randomnumber] .text);
            $('#cname')。html('-'+ quoteArray [randomnumber] .cname);

            cHeight = $('#customerquotes')。height()+ 50;
            AdjustHeight(bHeight,cHeight,divtoanim);

            $('#ctext')。delay(500).animate({'opacity':'1'},500);
            $('#cname')。delay(1500).animate({'opacity':'1'},500);
        });
    },15000);

最佳答案

这是一个异步问题:代码运行时,数组quoteArray为空,因为它会触发ajax请求并继续前进。依赖quoteArray的所有内容都应位于success$.ajax函数中。

当您在控制台中键入quoteArray.length时,该数组具有长度,这仅是因为那时Ajax请求已完成。

08-27 10:11