如何将setTimeout添加到随机MP3列表中

问题描述 投票:0回答:1

以下脚本在网页上可以很好地使从下面列表中播放的MP3随机播放,但是我希望MP3在页面加载后大约10秒后使用setTimeout开始播放。如何正确完成?

以下脚本在网页上可以很好地使从下面列表中播放的MP3随机播放,但是我希望MP3在页面加载后大约10秒后使用setTimeout开始播放。如何正确完成?

<script>
    var audio; 
    var playlist; 
    var tracks; 
    var current; 
    var musicarr = [

    "After the Sun.mp3",
    "Dreamlight.mp3",
    "Shadows of the moon.mp3",
                   ];    

    shuffle(musicarr);

    init();
    function init(){
        current = 0;
        audio = $('audio');
        audio[0].volume = .7;

        len = musicarr.length;

        run(musicarr[current], audio[0]);

        audio[0].addEventListener('ended',function(e){
            current++;
            if(current == len){
                current = 0;
            }
            run(musicarr[current],audio[0]);
        });
    }

    function run(link, player){
            player.src = link;
            audio[0].load();
            audio[0].play();    
    }

    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex ;

      // While there remain elements to shuffle...
      while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;


        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }

      return array;
    }

    </script> 
javascript arrays
1个回答
0
投票

删除您刚刚在函数init(){...}上方的init()调用,然后在脚本底部添加以下内容:

© www.soinside.com 2019 - 2024. All rights reserved.