准备好文档后未加载YouTube API框架吗?

时间:2019-01-18 13:21:19

标签: javascript jquery iframe youtube-api

我当前正在尝试触发一些函数,因为正在加载嵌入式youtube播放器时,但是由于某种原因,似乎不是正在加载API,还是基于API事件触发了回调函数状态?

我在做什么错了?

const $ = require('jquery');

$(document).ready( function() {
  console.log( "ready!" );
  loadPlayer();
});

function loadPlayer() { 
  if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {

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

    window.onYouTubePlayerAPIReady = function() {
      onYouTubePlayer();
    };

  } else {

    onYouTubePlayer();

  }
}

var player;

function onYouTubePlayer() {
  player = new YT.Player('player', {
    height: '490',
    width: '880',
    videoId: 'video',
    playerVars: { controls:1, showinfo: 0, rel: 0, showsearch: 0, iv_load_policy: 3 },
    events: {
      'onReady': window.onPlayerReadVar = function() {
        onPlayerStateChange();
      },
      'onStateChange': window.onPlayerStateChangeVar = function() {
        onPlayerStateChange();
      }
    }
  });
  console.log("youtubePlayerready")
}
  var done = false;

  function onPlayerStateChange(event) {

      console.log("onPlayerStateChange");
      done = true;
  }

页面加载后,我只能在控制台中看到“就绪”的输出。

我在这里做什么错了?

0 个答案:

没有答案