如何在javascript中随机播放声音
我有这样的声音:如何在javascript中随机播放声音,javascript,Javascript,我有这样的声音: a = new Audio('audio.mp3'); 当我想玩它时,我使用a.play() 我如何在随机时间播放此内容?试试以下方法: (function loop() { var rand = Math.round(Math.random() * (3000 - 500)) + 500; // A random value between 3 and 500 seconds setTimeout(function() { a.pl
a = new Audio('audio.mp3');
当我想玩它时,我使用a.play()
我如何在随机时间播放此内容?试试以下方法:
(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);
}());
来源:问题。setInterval和Math.random()是您需要的
a = new Audio('audio.mp3');
window.setInterval(function () {
a.play();
}, Math.random() * 500);
看看setTimeout()和Math.random()