简体   繁体   English

在计时器上显示当前视频时间(小时:分钟:秒)

[英]Show current video time on a timer (hours:minutes:seconds)

I have a simple timer which is displaying the current time of a video on my page.我有一个简单的计时器,它在我的页面上显示视频的当前时间。 Sadly it's limited to only showing minutes and seconds, if the video duration is longer than the timer can handle, it will at some point start displaying an incorrect value.遗憾的是,它仅限于显示分钟和秒,如果视频持续时间长于计时器可以处理的时间,它会在某个时候开始显示不正确的值。 After one hour, it will start displaying something like 61:01 (min:sec) And that's a real issue.一小时后,它将开始显示类似 61:01 (min:sec) 的内容,这是一个真正的问题。

 let video = document.querySelector('video'); let counter = document.querySelector('p'); let hrs, mins, secs; window.setInterval(() => { mins = Math.floor(video.currentTime / 60); secs = Math.floor(video.currentTime % 60); if (secs < 10) { secs = '0' + String(secs); } counter.textContent = mins + ':' + secs; },1000);
 <video controls src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" height='200px' width='330px'></video> <p></p>

Sadly I don't have any hour long videos to show the issue.... So could somebody please explain me how should I build this small function so it's capable of displaying hours?可悲的是,我没有任何长达一小时的视频来展示这个问题......所以有人可以解释我应该如何构建这个小功能以便它能够显示小时吗? Right now I only found a buggy and very unreliable way but it's too pathetic for being shown here.现在我只找到了一个有问题且非常不可靠的方式,但在这里展示太可悲了。

Do you like?你喜欢?

<video
  controls
  src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
  height="200px"
  width="330px"
></video>
<p></p>

<script>
  function secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor((d % 3600) / 60);
    var s = Math.floor((d % 3600) % 60);

    var hDisplay = h + ":";
    var mDisplay = m + ":";
    var sDisplay = s;

    return hDisplay + mDisplay + sDisplay;
  }

  let video = document.querySelector("video");
  let counter = document.querySelector("p");
  let hrs, mins, secs;

  window.setInterval(() => {
    counter.textContent = secondsToHms(video.currentTime);
  }, 0);
</script>

Happy codding.快乐编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM