如何在javascript中随机播放声音

时间:2014-12-10 23:23:34

标签: javascript

我有这个声音:

a = new Audio('audio.mp3');

当我想播放时我使用a.play() 我如何随机播放?

2 个答案:

答案 0 :(得分:2)

试试这个:

(function loop() {
    var rand = Math.round(Math.random() * (3000 - 500)) + 500; // A random value between 3 and 500 seconds

    setTimeout(function() {
            a.play(); // Play the audio
            loop(); // Call the loop function again
    }, rand);
}());

来源:this问题。

答案 1 :(得分:0)

您正在寻找

setInterval Math.random()

a = new Audio('audio.mp3');
window.setInterval(function () {
    a.play();
}, Math.random() * 500);
相关问题