javascript - 让 timeOut() 从数组中每秒播放一首歌曲

标签 javascript jquery

我正在尝试制作一款基于采样器的游戏。游戏的目的是复制使用随机数生成的序列。我有一个数组,其中包含随机生成的数字并使用 .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/

相关文章:

javascript - 准确的 jQuery x y 鼠标点击图像

javascript - 在没有事件监听器的情况下将跨度转换为链接

javascript - 标题基于不断变化的图像?

javascript - iPad、iPhone 模态对话框滚动问题

javascript - 如何检查 id 值是否已经存在于对象数组中?

javascript - 文件不被缓存

javascript - 无法重置 svg 路径的填充颜色?

jquery - 原型(prototype): Detect Chrome and Add Class

javascript - 优化jsp页面图片加载

javascript - Silverlight 如何在命名空间中调用 Javascript 函数?