Javascript 和 HTML5 循环问题

Javascript and HTML5 loop issue

我正在尝试在 HTML5 中创建一个简单的循环录制程序。我已经完成了一些基础知识,但我现在遇到的问题是,当我尝试递归播放音频时,它会在音频完成之前或之后一直触发该方法。

我试图设置一个 setTimeout,但这只会让我感到困惑,因为我 认为 将是一个正确的时间间隔,这让我更加困惑。有人可以看看这个吗?

您还可以找到一个活动站点http://rossb.byethost11.com/

        /*
         * finds duration of sound file in milliseconds
         */
        function getMasterDuration() {
          var sound = document.getElementById("session-1");
          var returnValue = sound.duration * 1000;
          return returnValue;
        }


        /*
         * called from the button
         */

        function playAllService() {
          //set up recursive loop to play sounds
          window.setInterval(recursivePlay, getMasterDuration);
        }

        function recursivePlay() {
            //play all sounds
            playAll();
            //set timeout until sound is done (?)
            setTimeout(recursivePlay, getMasterDuration);
          }
          /*
           * Finds all the recording sessions and plays them.
           */

        function playAll() {
          for (var i = 1; i <= numberOfSessions; i++) {
            play(i);
          }
        }

是的,我知道它需要清理。

感谢您的帮助!

编辑澄清:我希望它不间断地循环播放。像循环花瓣或鼓机。

当您访问函数时 没有 括号,您指的是函数本身。所以调用 recursivePlay returns 这个:

function recursivePlay() {
  //play all sounds
  playAll();
  //set timeout until sound is done (?)
  setTimeout(recursivePlay, getMasterDuration);
}

这对于 setTimeout 的第一个参数是正确的,因为您希望 函数 在间隔后 运行。

setTimeout 的第二个参数应该是一个数字(以毫秒为单位)。但是,您返回的是 function getMasterDuration 而不是函数的 result

要解决这个问题,只需将括号添加到 getMasterDuration:

setTimeout(recursivePlay, getMasterDuration());