Copy Code code as follows:
<script language= "JavaScript" >
<!--
var test = ["AA", "BB", "CC", "dd", "EE"];
document.write (Test[math.floor (Math.random () *test.length)]);
SetInterval ("Location.reload ()", 1000);
-->
</SCRIPT>
It's a wonderful way to do it. Suitable for the title of the nature of the text of the random rotation display.
There are two different ways to achieve this:
One, randomly take a single, two, let the whole array random sort
Note: The [] symbol defines an array in JavaScript, {} defines an object
To randomly get one of the arrays inside:
Copy Code code as follows:
<script type= "Text/javascript" >
Random get one of the arrays
var Arr = ["A", "B", "C", "D"];
var n = math.floor (Math.random () * arr.length + 1)-1;
Alert (Arr[n]);
</script>
Randomly sort the entire array of arrays:
Copy Code code as follows:
<script type= "Text/javascript" >
Randomly sort an entire array
var arr1=[1,2,3,4,5,6,7,8,9,10,22,33,55,77,88,99];
Arr1.sort (function () {return math.random () >0.5?-1:1;});
alert (ARR1);
</script>
==========================================
PHP has a very convenient to disrupt the array of functions shuffle (), this function in many cases will be used, but the JavaScript array does not have this method, it does not matter, you can expand A, do-it-yourself, well-clothed.
Copy Code code as follows:
<script type= "Text/javascript" >
<! [cdata[
var shuffle = function (v) {
For (var j, x, i = v.length; i; j = parseint (Math.random () * i), x = V[--i], v[i] = V[j], v[j] = x);
return v;
};
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write ("a =", A.join (","), "<br><br>shuffle (a) =", Shuffle (a));
if (! Array.prototype.shuffle) {
Array.prototype.shuffle = function () {
For (var j, x, i = this.length; i; j = parseint (Math.random () * i), x = This[--i], this[i] = This[j], this[j] = x);
return this;
};
}
document.write ("<br>a.shuffle () =", A.shuffle ());
]]>
</script>