0

重複の可能性:
配列内の要素をランダム化していますか?

この「Quote Rotator」スクリプトをウェブで見つけましたが、それはうまく機能しますが、リストを循環するだけでなく、引用が表示される順序をランダムにしたいと思います。どんな助けでも大歓迎です。

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();
4

1 に答える 1

1

回転引用符を次のように変更します。

function rotatequote()
{
    var thequote = myquotes[( Math.floor ( Math.random() * myquotes.length ) )];
    document.getElementById('quotetext').innerHTML = thequote;
    var t=setTimeout("rotatequote()",10000);
}
于 2012-07-08T00:27:29.210 に答える