2012-07-08 71 views
0

可能重复:
Randomizing elements in an array?随机化数组中的引号?

我在网上找到了这个“报价肩”的剧本,它工作得很好它做什么,但我想以随机的顺序引号只显示在列表中循环显示。任何帮助,将不胜感激。

var myquotes = new Array(
    'Quote #1', 
    'Quote #2', 
    'Quote #3' // Leave the last quote without a comma at the end 
    ); 

function rotatequote() 
{ 
    thequote = myquotes.shift(); //Pull the top one 
    myquotes.push(thequote); //And add it back to the end 

    document.getElementById('quotetext').innerHTML = thequote; 
    t=setTimeout("rotatequote()",10000); 
} 

// Start the first rotation. 
rotatequote(); 

回答

1

更改rotateQuote到:

function rotatequote() 
{ 
    var thequote = myquotes[(Math.floor (Math.random() * myquotes.length))]; 
    document.getElementById('quotetext').innerHTML = thequote; 
    var t=setTimeout("rotatequote()",10000); 
} 
+0

这可能连续显示相同报价的两倍 - 这听起来像他希望能有一个洗牌 – mpen 2012-07-08 00:29:58

+0

啊,我误解了他想要的东西。 – Lusitanian 2012-07-08 00:30:20

+0

他确实表示他想随机化报价显示的顺序。当你随机化某些东西时,你有可能连续多次选择相同的物品。你可以每次洗牌数组,但是如果它真的是一个随机洗牌,同一个物品可能会被洗到阵列的前面。 – HeatfanJohn 2012-07-08 00:37:23