我有一个HTML5视频,我设置为一个给定的循环间隔,一按按钮。
用户应该能够通过单击文档中的任何位置退出循环。视频应该暂停。
使用我当前的代码,单击事件只偶尔注册,或者根本不注册。
HTML:
<body>
<video src="./example.mp4" width="480" type="video/mp4" controls></video>
<button>Test</button>
</body>
JS:
$(document).ready(function() {
var video = document.getElementsByTagName('video')[0];
var timeStamp = [7, 8];
video.addEventListener('canplay', execute);
function execute() {
video.removeEventListener('canplay', execute);
$('button').click(function() {
playVideoInterval(timeStamp[0], timeStamp[1]);
});
function playVideoInterval(start, end) {
video.addEventListener('timeupdate', loop);
video.currentTime = start;
document.body.addEventListener('click', endLoop, true);
video.play();
function loop() {
if (this.currentTime >= end) {
video.currentTime = start;
video.play();
}
}
function endLoop() {
video.pause();
document.body.removeEventListener('click', endLoop);
video.removeEventListener('timeupdate', loop);
console.log('hi');
}
}
}
});