HTML5video时间跟踪
我有一个HTML5video在我的网站上播放,我想跟踪这个video的时间,让它在video持续时间持续10秒,20秒,30秒等时执行function或警报。我有点儿难过怎么做? 我可以用jQuerys setInterval每10秒发出一次警报,但这是基于jquerys时间参数,而不是电影本身…这就是我所拥有的
window.setInterval(function(){ var video = $('video#full-vid').get(0); //get the native browser source alert('video time:' + video.currentTime); }, 10000);
有任何想法吗?
基本上这是逻辑:
- 获取video时间
- 如果10秒的video通过警告“10秒已经过去”
- 如果20秒的video通过警告“20秒已经过去”等等……
谢谢
你应该能够像这样做
// set event listener to execute on timeupdate. This gets invoked every ~250ms or so $('video#full-vid').on('timeupdate',function() { // use parseInt to round to whole seconds var ct = parseInt(this.currentTime); // only eval once per second inc, since timeupdate pops ~4 times per second if (this.lastTime!=ct) { // if current time is divisible by 10 then an inc of 10s has passed if (ct%10===0) { console.log(ct,'seconds have passed'); } } this.lastTime=ct; });
编辑:如果您希望在特定时间间隔执行此操作,则可以执行以下操作:
// set event listener to execute on timeupdate. This gets invoked every ~250ms or so $('video#full-vid').on('timeupdate',function() { // use parseInt to round to whole seconds var ct = parseInt(this.currentTime); // only eval once per second inc, since timeupdate pops ~4 times per second if (this.lastTime!=ct) { // do something at specified time elapsed markers switch (ct) { case 10 : // do something break; case 20 : // do something break; } } this.lastTime=ct; });