我想知道是否可以在实际时间内暂停视频?
我测试过,视频总是在 x.xxx
暂停,而不是 x.00
video.load();
video.play();
video.ontimeupdate = function(){
if(this.currentTime >= 3) {
video.pause();
}
};
演示: https://jsfiddle.net/l2aelba/4gh7a058/
有什么技巧吗?
PS:也应该有尽可能好的性能
最佳答案
我相信您无法保证视频会在准确的时刻停止。
根据documentation对于媒体 Controller :
Every 15 to 250ms, or whenever the MediaController’s media controller position changes, whichever happens least often, the user agent must queue a task to fire a simple event named timeupdate at the MediaController.
timeupdate 事件将在可以使用最不频繁的场景时触发。它不允许您选择更新的确切触发时间。
您可以采取的技巧如下:删除 timeupdate 事件,设置您自己的间隔并使用该间隔检查时间。
setInterval(function () {
var ct = video.currentTime;
current_time_el.innerHTML = 'Current time : ' + ct;
if(ct >= 3) {
video.pause();
}
}, 40);
不过,这种方法将迫使您对代码更加小心。 (例如,当不再需要时使用clearInterval()清理你的间隔)
关于javascript - 如何实时暂停/停止视频(例如 3.00 秒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35890033/