Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查视频是否连续播放30秒而不考虑起点?_Javascript - Fatal编程技术网

Javascript 如何检查视频是否连续播放30秒而不考虑起点?

Javascript 如何检查视频是否连续播放30秒而不考虑起点?,javascript,Javascript,我有一个HTML格式的视频元素 <video id="video_player" controls> <source src="{{object.video_file.url}}" type="video/mp4"> Your browser does not support the video tag. </video> 您需要跟踪视频的开始、停止或暂停时间。让我们将其存储在变量中:

我有一个HTML格式的视频元素

<video id="video_player" controls>
  <source src="{{object.video_file.url}}" type="video/mp4">
    Your browser does not support the video tag.
</video>

您需要跟踪视频的开始、停止或暂停时间。让我们将其存储在变量中:

var startTime=new Date()

设置重置计时器的功能:

function reset() {
  startTime = new Date();
}
$('#video_player')[0].addEventListener('play', reset);
$('#video_player')[0].addEventListener('pause', reset);
$('#video_player')[0].addEventListener('stop', reset);
每当用户启动、停止或暂停视频时,请重置计时器:

function reset() {
  startTime = new Date();
}
$('#video_player')[0].addEventListener('play', reset);
$('#video_player')[0].addEventListener('pause', reset);
$('#video_player')[0].addEventListener('stop', reset);
最后,在播放视频时,检查播放时间:

$('#video_player')[0].addEventListener('timeupdate', (e) => {
  let elapsedSeconds = ( new Date() - startTime ) / 1000;
  console.log( `Video has been playing for ${elapsedSeconds} seconds` );
  if ( elapsedSeconds > 30 ) {
    console.log( 'Video has been playing for more than 30 seconds!' );
    // Do something special
  }
});