video.currentTime 事件绑定出现问题

Trouble with video.currentTime event binding

本文关键字:问题 绑定 currentTime 事件 video      更新时间:2023-09-26

我想在视频达到 5 秒或更长时间后触发事件,但我的代码遇到了一些问题。

.HTML:

<video id="video1" controls>
    <source src="media/video.m4v" type="video/mp4" />
    <source src="media/video.webm" type="video/webm" />
    <source src="media/video.ogg" type="video/ogg" />
    <p> Your browser does not support the HTML5 video feature. </p>
</video>

Javascript:

 $(function () {
    var video = $('#video1');
    var time = 5;
    function init () {
        alert('Video is not available.');
    }
    $(video).on('timeupdate', function () {
        if (video.currentTime >= time) {
            init();
        }
    });
 });

不幸的是,这不是一个有效的代码,有人知道我哪里出错了...?

你会得到undefined,因为视频是没有属性currentTime的jQuery对象,你可以像这样得到这个值

video.get(0).currentTime

this.currentTime

 $(function () {
    var video = $('#video1');
    var time = 5;
    function init () {
        alert('Video is not available.');
    }
    video.on('timeupdate', function () {
        if (video.get(0).currentTime >= time) {
            init();
        }
    });
 });