<!-- Видео без контролов -->
<video>
<source src="URL" id="myVideo">
</video>
<script>
var vid = document.getElementById("myVideo");
vid.ontimeupdate = function() {myFunction()};
function myFunction() {
event.PreventDefault();
}
</script>
Т.к. ontimeupdate срабатывает примерно каждые полсекунды во время проигрывания, то ориентироваться надо не на него, а на событие seeking. Вот рабочий код.
var video = document.getElementById('video');
var supposedCurrentTime = 0;
video.addEventListener('timeupdate', function() {
if (!video.seeking) {
supposedCurrentTime = video.currentTime;
}
});
// prevent user from seeking
video.addEventListener('seeking', function() {
// guard agains infinite recursion:
// user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....
var delta = video.currentTime - supposedCurrentTime;
if (Math.abs(delta) > 0.01) {
console.log("Seeking is disabled");
video.currentTime = supposedCurrentTime;
}
});
video.addEventListener('ended', function() {
// reset state in order to allow for rewind
supposedCurrentTime = 0;
});