Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在数组中为每个项目实现一组指令?_Javascript_Arrays - Fatal编程技术网

Javascript 如何在数组中为每个项目实现一组指令?

Javascript 如何在数组中为每个项目实现一组指令?,javascript,arrays,Javascript,Arrays,我不知道该怎么说,但我有一系列的歌曲,我需要播放它们,但在阵列完成后,它需要循环。 我需要它看起来像这样: let playlist = [ { name: "value" }, { name: "value" } ] function myFunc() { console.log(playlist[0]); // Log the first song console.log(playlist[1]); // Af

我不知道该怎么说,但我有一系列的歌曲,我需要播放它们,但在阵列完成后,它需要循环。 我需要它看起来像这样:

let playlist = [
    {
        name: "value"
    },
    {
        name: "value"
    }
]

function myFunc() {
    console.log(playlist[0]); // Log the first song
    console.log(playlist[1]); // After the first song finishes playing log this song
    // Once finished with this, repeat again.
}
它基本上是播放一个播放列表,并将其设置为重复播放。
谢谢

让我们试试这个,让我知道:

let playlist = [
    {
        name: "value",
        src: "song.wave",
    },
    {
        name: "value",
        src: "sg.wave",
    }
];

function getDuration(src){
    var music = new Audio(src);
    music.addEventListener('loadeddata', () => {
  let duration = music.duration;
  return duration;
  });
}

function playThePlaylist(playlist) {
    let srcSongs = playlist.map(song => song.src);
    let counterSongsPlayed = 0;
      var durationSong = getDuration(song);
      song.addEventListener('loadeddata', () => {
       setInterval(function(){
        if(durationSong == song.currentTime || counterSongsPlayed == 0){
            var song = new Audio(srcSongs[counterSongsPlayed]);
                song.play();
            counterSongsPlayed++;
         }

         if(counterSongsPlayed > srcSongs.length)
           counterSongsPlayed = 0;
       }, 1000);
      });
}

你基本上想一次又一次地调用你的播放列表功能,据我所知。因此,您可以使用具有时间间隔的setinterval函数,在该时间间隔之后,您希望再次调用该函数,如下所示: 您必须使用按钮之类的东西来调用该函数

    <!DOCTYPE html>
<html>
<body>

<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<p>After clicking away the alert box, an new alert box will appear in 3 seconds. This goes on forever...</p>
<button onclick="myFunc()">Try it</button>

<script>
let playlist = [
    {
        name: "value"
    },
    {
        name: "value"
    }
]

function myFunc() {
   setInterval(function(){ console.log(playlist[0]); console.log(playlist[1]);}, 3000);  // Log the first song
  // After the first song finishes playing log this song
    // Once finished with this, repeat again.
}


</script>

</body>
</html>

希望这有帮助。

你用什么来播放歌曲?只是另一个功能而已。将其与Discord.js结合使用,作为全天候音乐机器人。