从timeupdate的整个视频开始时,如何以10秒的间隔调用功能?

发布于 2025-02-03 19:15:15 字数 492 浏览 2 评论 0原文

我的目标是每当用户在视频中超过10秒的间隔标记时,要发射事件,例如10、20、20、30、40秒...

我如何使用timeupdate 事件?我觉得自己很亲密,但似乎无法得到正确的答案。到目前为止,我目前的时间为〜10.250,〜10.5和〜10.75。我只需要每10秒间隔一次

videoPlayer.on('timeupdate', (e) => {
  // Only invoke the function after the 10 second mark
  if (videoPlayer.currentTime() > 10 && videoPlayer.currentTime() % 10 < 1) {
    myFunc();
  }
});

感谢我能获得的任何帮助!谢谢你!

My goal is to fire an event once every time the user gets past a 10 second interval mark within their video e.g. 10, 20, 30, 40 seconds...

How can I achieve this with the timeupdate event? I feel like I'm close but can't seem to get the right answer. What I have so far is firing several times at ~10.250, ~10.5, and ~10.75 current time. I need it to only fire once per 10 second interval.

videoPlayer.on('timeupdate', (e) => {
  // Only invoke the function after the 10 second mark
  if (videoPlayer.currentTime() > 10 && videoPlayer.currentTime() % 10 < 1) {
    myFunc();
  }
});

I appreciate any help I can get! Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-02-10 19:15:15

尝试在几秒钟内跟踪当前时间,并在更改时才更新。例如:

// keep track of time in seconds
let time = 0;
videoPlayer.addEventListener('timeupdate', (e) => {
  const exactTime = videoPlayer.currentTime;
  const current = Math.floor(exactTime);

  // still in the same second! do nothing
  if (current === time) return;
  
  time = current;

  // if it is a new ten second interval
  if (time % 10 === 0) {
    // console.log(exactTime);
    myFunc();
  }
});

Try keeping track of the current time in seconds and updating only when it changes. For example:

// keep track of time in seconds
let time = 0;
videoPlayer.addEventListener('timeupdate', (e) => {
  const exactTime = videoPlayer.currentTime;
  const current = Math.floor(exactTime);

  // still in the same second! do nothing
  if (current === time) return;
  
  time = current;

  // if it is a new ten second interval
  if (time % 10 === 0) {
    // console.log(exactTime);
    myFunc();
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文