等待事件未触发

Waiting event not firing

我不明白为什么等待事件没有触发。

基本上我希望容器中的动画在视频缓冲时工作。

我还需要注意其他事件吗?

function play() {
  var elem = document.getElementById("animate");
  var pos = 0;
  var id = setInterval(frame, 5);

  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++;
      elem.style.top = pos + 'px';
      elem.style.left = pos + 'px';
    }
  }
}
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}

#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
}
<video onwaiting="play()" controls autoplay>
   <source src = "bleach-cool-mp3.webm" type="video/webm" >
</video>
<div id="container">
  <div id="animate"></div>
</div>

两件重要的事情:

首先:你不能给你的函数命名play(),因为是视频api的reserved keyword,你必须重命名

第二:您的 onwaiting 事件运行良好,但它仅在您的视频必须缓冲下一帧时触发(例如连接速度慢的问题)。如果您希望它在视频搜索要显示的数据时触发,例如第一次加载,请使用 onloadstart 事件:

下面是您的工作片段:

function PlayAnimation() {
  var elem = document.getElementById("animate");
  var pos = 0;
  var id = setInterval(frame, 5);

  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++;
      elem.style.top = pos + 'px';
      elem.style.left = pos + 'px';
    }
  }
}
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}

#animate {
  width: 50px;
  height: 50px;
  position: absolute;
  background-color: red;
}
<video onwaiting="PlayAnimation()" onloadstart="PlayAnimation()" controls autoplay>
   <source src = "https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4" >
</video>
<div id="container">
  <div id="animate"></div>
</div>