簡體   English   中英

如何在JavaScript中隨機播放聲音

[英]How to play a sound at random time in 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.play(); // Play the audio
            loop(); // Call the loop function again
    }, rand);
}());

資料來源: 這個問題。

您正在尋找setIntervalMath.random()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM