Javascript 代码会运行,因为它会发出ajax请求,然后继续运行。任何依赖于quotarray的内容都应该在$.ajax的success函数中

Javascript 代码会运行,因为它会发出ajax请求,然后继续运行。任何依赖于quotarray的内容都应该在$.ajax的success函数中,javascript,random,while-loop,infinite-loop,Javascript,Random,While Loop,Infinite Loop,当您在控制台中键入quotarray.length时,数组有一个长度,这仅仅是因为此时Ajax请求已经完成。什么是rotationArray以及如何重置循环?您如何准确声明此数组?如果未使用var quotarray=[]定义“quotarray”,则切到它;或var quotarray=new Array()那么它可能根本不是数组。rotationArray是一个存储为每个周期生成的随机数的数组。quoteArray声明如下:var quoteArray=new array();我确信数组长度

当您在控制台中键入
quotarray.length
时,数组有一个长度,这仅仅是因为此时Ajax请求已经完成。

什么是
rotationArray
以及如何重置循环?您如何准确声明此数组?如果未使用
var quotarray=[]定义“quotarray”,则切到它;
var quotarray=new Array()
那么它可能根本不是数组。rotationArray是一个存储为每个周期生成的随机数的数组。quoteArray声明如下:var quoteArray=new array();我确信数组长度被正确找到,因为我还使用了console.log(quoteArray.length),它返回的数字大于0。quoteArray.length将取决于函数循环的次数。我不是在写“console.log(randomnumber)”在firebug中,我只是在代码中内联编写了它,所以每次它在while循环中迭代时,我都会得到“randomnumber”是什么的读数。@用户是的,即使是这样。设置
async:false
将导致浏览器阻止所有其他内容,但
$之后的脚本仍将运行。ajax
仍将运行。
async:false
通常是ally不是您要找的。@用户,如果您放入
console.log(quoteArray.length)在同一个循环中,你应该得到
0
@box9谢谢你的澄清!我有一个错误的印象。我猜另一件事是a)这个函数在页面加载后15秒才运行,b)通过firebug查看ajax请求,在运行之前它们都已完成,所以我理解你的意思回答:我不明白它在这种情况下是如何应用的。同样值得注意的是:如果我一开始没有说清楚,这个脚本有时会成功运行(在Chrome、Firefox和IE中)。我似乎只有在Firefox和IE中存在无限循环的问题,但在Chrome中没有。
quoteArray[1] = "some text"
quoteArray[2] = "some different text"
quoteArray[3] = "text again"
quoteArray[4] = "completely different text"
quoteArray[5] = "ham sandwich"
//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));
}
// Function to initialize the quoteObj function quoteObj(text,cname,ccompany,url,height) { this.text=text; this.cname=cname; this.ccompany=ccompany; this.url=url; this.height=height; } // Populate my quotes Object with the quotation information from the XML sheet. var qObj = new quoteObj('','','',''); var quoteArray = new Array(); var counter = 0; //cycles through each XML item and loads the data into an object which is then stored in an array $.ajax({ type: "GET", url: "quotes.xml", dataType: "xml", success: 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(); ++counter; }); } }); // This is the setion that is generating my infinite loop issue. // I've included all of the other code in case people are wondering specific things about how an item was initialized, etc. // Generate a random first quote then randomly progress through the entire set and start all over. 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(); //NOT RELATED// // Give the innershadow a height so that overflow hidden works with the quotations. $(divtoanim).css({'height' : bHeight}); // Rotate the Quotations Randomly function. setInterval(function(){ randomnumber = Math.floor(Math.random() * (quoteArray.length)); //checks to see if the function loop needs to start at the beginning. if(v == (quoteArray.length)){ rotationArray.length = 0; v = 0; } //determines if the random number is both different than any other random number generated before and that is is not the same as the last random number while(randomnumber === rotationArray[randomnumber] || randomnumber === lastnumber){ randomnumber = Math.floor(Math.random() * (quoteArray.length)); } lastnumber = randomnumber; rotationArray[randomnumber] = randomnumber; ++v; //NOT RELATED// //animation sequence $('#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);
Math.floor(Math.random() * (5));
var results = ['some text','some text2','some text3','some text4','some text5', /* ...etc */ ],
       randomable = results;

function getRandomOffset( arr )
{
    var offset,
           len;

    if( arr.length < 1 )
        return false;
    else if( arr.length > 1 )
        offset = Math.floor(Math.random() * arr.length );
    else
        offset = 0;

    arr.splice( offset, 1 );

    return [
        offset,
        arr
    ];

}

while( res = getRandomOffset( randomable ) )
{
    // Set the randomable for next time
    randomable = res[1];
    // Do something with your resulting index
    results[ res[0] ];
}