html5视频事件侦听器未获取当前时间
关于我之前的问题 - 查找与视频 currentTime 相关的文本行并使用 javascript 突出显示它? - 我一直在尝试设置视频事件侦听器以使用 video.currentTime 设置 now 变量的值。这是我当前拥有的代码(没有返回错误但实际上不起作用)。
function transcript(){
var now;
var lines = document.getElementById("transcript").getElementsByTagName("span");
function timeupdate(){
var video = document.getElementsByTagName('video')[0];
video.addEventListener('currentTime', tran, false);
now = video.currentTime;
}
function tran(){
for (var i = 0, l = lines.length; i < l; i++) {
if (now >= +lines[i].getAttribute("data-start") &&
now <= +lines[i].getAttribute("data-end")) {
lines[i].className = "current";
} else {
lines[i].className = "";
}
}}
}
我知道 tran 函数可以工作,但不起作用的是更改 now 变量。我还尝试打印视频的当前时间,但这也不起作用,所以我想我没有正确执行事件监听器? 谢谢
In relation to my earlier question - Find line of text in relation to a video currentTime and highlight it using javascript? - I have been trying to set the video eventlistener to set the value of the now variable with the video.currentTime. This is the code I currently have (without it returning errors but not actually working).
function transcript(){
var now;
var lines = document.getElementById("transcript").getElementsByTagName("span");
function timeupdate(){
var video = document.getElementsByTagName('video')[0];
video.addEventListener('currentTime', tran, false);
now = video.currentTime;
}
function tran(){
for (var i = 0, l = lines.length; i < l; i++) {
if (now >= +lines[i].getAttribute("data-start") &&
now <= +lines[i].getAttribute("data-end")) {
lines[i].className = "current";
} else {
lines[i].className = "";
}
}}
}
I know that the tran function works, but what isn't working is changing the now variable. I also tried to print the current time of the video but that hasn't worked either, so I guess I'm not doing the eventListener properly?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
now 是在调用 timeupdate 时计算的,而不是在调用 tran 时计算的。因此,如果您调用 tran,那么 now 将是调用 timeupdate 的时间。
要获得准确的 now 变量,请在 tran 函数中获取当前时间:
now is getting calculated when timeupdate is getting called, not when tran is getting called. So if you call tran, now is going to be at the time that timeupdate was called.
To get an accurate now variable, grab the current time in the tran function: