HTML5-播放视频时每隔几秒钟触发一次事件 [英] HTML5 - Fire event every few seconds while video is playing

查看:473
本文介绍了HTML5-播放视频时每隔几秒钟触发一次事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过跟踪在不同时间间隔掉多少用户来跟踪视频的用户参与度.

I want to track user engagement for a video, by tracking how many users are dropping of at different time interval.

为此,我需要在视频播放时每15秒触发一次跟踪事件.

For this I need to fire a tracking event, every 15 seconds while the video is playing.

playing事件被触发一次.我需要在视频的整个生命周期中都可以使用的东西.

The playing event is triggered once. I need something which I can use throughout the life of the video.

var _video = $('video')[0],
        timeoutID;
    $(_video).on('playing', function(event){
        timeoutID = window.setTimeout(function() {
            console.log("video is playing");
        }, 15000);
    }).on('ended', function(event){
        console.log("video eneded");
        window.clearTimeout(timeoutID);
    });

推荐答案

使用window.setInterval代替window.setTimeout

$(_video).on('playing', function(event){
        timeoutID = window.setInterval(function() {
            console.log("video is playing");
        }, 15000);
    }).on('ended', function(event){
        console.log("video eneded");
        window.clearInterval(timeoutID);
    });

这篇关于HTML5-播放视频时每隔几秒钟触发一次事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆