开发者

Select a random string from an array [duplicate]

开发者 https://www.devze.com 2023-04-04 01:37 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: JavaScript: Getting random value from an array
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

JavaScript: Getting random value from an array

Could someone help me on this topic? I've got this code.

var textArray = [
    'song1.ogg',
    'song2.ogg'
]
audioElement.setAttr开发者_如何学JAVAibute('src', textArray);

How can I randomly get one of those strings into my audio element?

Would be glad if someone can help....


var textArray = [
    'song1.ogg',
    'song2.ogg'
];
var randomNumber = Math.floor(Math.random()*textArray.length);

audioElement.setAttribute('src', textArray[randomNumber]);


var randomIndex = Math.floor(Math.random() * textArray.length); 
var randomElement = textArray[randomIndex];
0

精彩评论

暂无评论...
验证码 换一张
取 消