2016-06-11 52 views
0

这是我HTML文件中的工作播放器。我希望YT.PlayerState.ENDED也可以引用一个存储视频ID的客户端Javascript对象(booth),但booth未定义。做这样的事情的正确方法是什么?YouTube iframe API - 从onPlayerStateChange事件处理程序中引用Javascript对象

function onPlayerStateChange(event) { 
    if (event.data == -1) { 
    player.playVideo(); 
    } 
    if (event.data == 0) { 
    alert("This alert pops up"); 
    alert(window.booth); 
    // Undefined -- even though "booth" is already instantiated in linked JS file 

    /* 
    window.booth.cue.index++; 
    event.target.loadVideoById(window.booth.cue.list[booth.cue.index].id); 
    */ 
    } 
} 

这里是整个Javascript file - 线16 socket.on('boothCreated'...就是booth被实例化后,才创建的球员处理器 - 所以它似乎对我来说,玩家应该能参考这一点。

+0

你能提供链接的JS文件吗?浏览器从不错。 – Azamantes

+0

@Azamantes,我只是标记你,如果你没有注意到我添加了JS文件。 – aweeeezy

回答

1

我不需要看任何进一步,链接JS文件的开头说的一切:

window.onload = function() { // beginning of function closure 
    var user = null; // these are private variables inside the function 
    var booth = null; // these are private variables inside the function 
    // etc... 

如果你想这些变量从window.booth可见等,那么你需要做的:

window.onload = function() { // beginning of function closure 
    window.user = null; // these are now public 
    window.booth = null; // these are now public 
    // etc... 

你只是把它们变成私人的,因此在'外部世界'中是看不见的。

或者,如果您想在保持私密的同时使用它们,那么您需要以某种方式加入文件并将onPlayerStateChange函数放入window.onload = function() { ...

+0

谢谢 - 你真棒!我很尴尬,我不知道这样的初级JS事情。 – aweeeezy

+0

别担心,我们都在那里:) – Azamantes

相关问题