嘿所以我想按百分比监控视频播放的进度,以便当观看25,50和75%的用户发送提醒时。这就是我到目前为止所拥有的。
var Currenttime1 = document.getElementById('video').attr('currenttime');
var Duration1 = document.getElementById('video').attr('duration');
console.log(Currenttime1);
console.log(Duration1);
function percenttime(){
var timemonitored = Duration1/Currenttime1 *100;
var alertsSent = 0;
if(timemonitored > 25 && alertsSent == 0)
{
console.log(timemonitored);
alert("player has reached 25%");
alertsSent++;
}
else if(timemonitored > 50 && alertsSent == 1)
{
alert("player has reached 50%");
alertsSent++;
}
else if(timemonitored > 75 && alertsSent == 2)
{
alert("player has reached 75%");
alertsSent++;
}
}
有什么我想念的吗?我输入console.log来查看Duration1,Currenttime1和timemonitored中的内容,但它没有填充值。这是小提琴链接。 http://jsfiddle.net/Ypczm/
答案 0 :(得分:1)
一种方法是创建一个间隔函数,每隔1000ms触发一次,并使用jquery获取当前播放时间。
编辑:使用jQuery工作JS:
$(function() {
var vid = $('#video'),
check,
reached25 = false,
reached50 = false,
reached75 = false;
vid.bind("play", function(event) {
var duration = vid.get(0).duration;
check = setInterval(function() {
var current = vid.get(0).currentTime,
perc = (current / duration * 100).toFixed(2);
if (Math.floor(perc) >= 25 &&! reached25) {
console.log("25% reached");
reached25 = true;
}
console.log(perc);
}, 1000);
});
vid.bind("ended pause", function(event) {
clearInterval(check);
});
});
答案 1 :(得分:-1)
您有多种语法错误:
console.log(Currenttime1)
^--missing semi-colons (and on other lines as well)
console.log(duration1)
^---not capitalized
如果你看着浏览器的Javascript控制台(例如firefox中的shift-ctrl-J),你会看到JS语法错误,这些错误已经完全杀死了整个代码块。
...
现在我看到你隐身编辑了D
v。 d
错误,但无论如何......分号业务仍然存在。