我正在尝试制作一款基于采样器的游戏。游戏的目的是复制使用随机数生成的序列。我有一个数组,其中包含随机生成的数字并使用 .each() 我试图让每个值播放一个正在工作但同时播放所有样本的样本。我已经得到了每次播放一秒钟的样本,但我需要的是一个样本,每秒按顺序播放。
这是我目前所拥有的:
$('#play').on("click", function(){
startGame();
message("Player 1 will start. You have 3 lives remaining. Input the correct sequence so you dont get booed off stage...Everyone is watching");
$.each(generatedArray, function(index, value){
var audio = new Audio("Sounds/" + value + ".wav");
audio.play(audio.duration * 1000);
});
谢谢
最佳答案
你可以这样做:
$('#play').on("click", function(){
startGame();
message("Player 1 will start. You have 3 lives remaining. Input the correct sequence so you dont get booed off stage...Everyone is watching");
$.each(generatedArray, function(index, value){
//here 1000 * index will run the song at the time interval
window.setTimeout(function(){
var audio = new Audio("Sounds/" + value + ".wav");
audio.play(audio.duration * 1000);
}, 1000*index);
});
现在在这种情况下,第一首歌曲将播放@1000*0 秒,接下来@1000*1,接下来@1000*2,依此类推...
希望这对您有所帮助!
关于javascript - 让 timeOut() 从数组中每秒播放一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35114059/