Using JS to implement an element _javascript technique of random return array

Source: Internet
Author: User
Tags shuffle javascript array
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.