且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

*** player.destroy();抛出'this.a为null',即使在验证玩家时也是如此

更新时间:2023-09-02 20:53:52

这里的问题是玩家不是未定义。发生了什么事情,你有一个全球玩家参考,你正在做以下事情:

The problem here is player is NOT undefined. What's happening is you have a global player reference, and you're doing the following with it:


  1. 在第一个面板中创建一个玩家

  2. 第一个面板关闭时销毁它

  3. 当第二个面板关闭时,在已经销毁的播放器(从第一个面板)上调用player.stopVideo()

目前,播放器包含对您最后一位***播放器的引用即使该玩家已经被摧毁,也可以使用is。

Currently, player holds a reference to whatever the last *** player you were using is, even if that player has already been destroyed.

你应该做的是在你摧毁它时清除对玩家的引用。毁灭不会(也不可能)这样做。您还可以简化if条件,因为!player 将自行检查null和undefined:

What you should be doing is clearing out your reference to the player when you destroy it. Destroy won't (and can't) do that. You can also simplify your if condition since !player will check for null and undefined on its own:

var resetView = function() {
  // If a *** player is active, make sure we stop it.
  if (!player) {
    console.log("Player could not be found.");
  } else {
    player.stopVideo();
    player.destroy();
    player = null;  // Clear out the reference to the destroyed player
  }