假设我们的应用程序在Safari上使用默认的视频播放器.

当用户播放视频,然后试图使用搜索栏移动到视频的不同位置时,似乎首先触发了pause个事件,然后我们将触发seekingseeked个事件.

I am wondering if we can get the 100 value prior to the seek.例如,假设用户使用搜索栏从t = 7跳到t = 42,我想以某种方式得到7作为currentTime值.

我希望我们可以通过访问pause事件处理程序中的currentTime属性来获得这个值,pause事件处理程序在查找之后立即调用,如下所示:

const video = document.querySelector('#myvideo');
video.addEventListener('pause', () => {
  // I expected that the `video.currentTime` here has the "previous" position,
  // but it already points to the new position
  console.log(video.currentTime);
});

但不幸的是,当时currentValue已经更新为新值.

有什么好方法可以实现吗?

(编辑)

<!DOCTYPE html>
<html>
  <body>
    <video
      id="myvideo"
      width="640"
      height="360"
      controls
      src="video.mp4"
    ></video>
  </body>
  <script>
    const video = document.querySelector("#myvideo");
    let cache = 0;

    video.addEventListener("timeupdate", () => {
      cache = video.currentTime;
    });

    video.addEventListener("pause", () => {
      console.log({ cache, currentTime: video.currentTime });
    });
  </script>
</html>

推荐答案

我认为"kaido"在说"Cache two values"的时候是这个意思

<script>
    const video = document.querySelector("#myvideo");
    let cache = 0;
    let cache_prev = 0;

    video.addEventListener("timeupdate", () => {
      cache_prev = cache; //# save last known value
      cache = video.currentTime; //# before updating to new currentTime
    });

    video.addEventListener("pause", () => {
    console.log("cache_prev : " + cache_prev );
    console.log("cache : " + cache );
    console.log("currentTime : " + video.currentTime );
    });
</script>

Javascript相关问答推荐

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

格式值未保存在redux持久切片中

调用removeEvents不起作用

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

MongoDB中的引用

将现场录音发送到后端

在286之后恢复轮询

将核心模块导入另一个组件模块时存在多个主题

如何发送从REST Api收到的PNG数据响应

自定义图表工具提示以仅显示Y值

无法检索与Puppeteer的蒸汽游戏的Bundle 包价格

如何组合Multer上传?

如何修复错误&语法错误:不能在纯react 项目中JEST引发的模块&之外使用导入语句?

如何在Jest中模拟函数

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?

Angel Auth Guard-用户只有在未登录时才能访问登录页面,只有在登录时才能访问其他页面

使用Java脚本在div中创建新的span标记

当一条路由在Reaction路由中命中时,如何有条件地渲染两个组件或更改两个插座?

如何在预览中显示html+css与数据URL?

如何使用Reaction/Redux创建购物车逻辑?