Youtube Player不会在setTimeout上启动,需要帮助

时间:2013-07-02 17:37:01

标签: javascript api youtube settimeout

我在构建播放器时遵循了Youtube API。我需要通过javascript调用autoplay函数,但它不会监听setTimeout函数,并且它不会开始播放:

有人可以点亮我错在哪里吗?

问候!

var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
       events: {
        'onReady': onPlayerReady,
      }

    });
  }

  function onPlayerReady(event){
  setTimeout(function(){

  playVideo();

  },5000);

  }

1 个答案:

答案 0 :(得分:-1)

当您声明player变量并稍后将其设置为YT.Player的实例时,您必须在setTimeout上使用相同的变量。

setTimeout(function(){ 
    player.playVideo();
},5000);