2010-11-28 86 views
1

關於我早先的問題 - Find line of text in relation to a video currentTime and highlight it using javascript? - 我一直在試圖設置視頻eventlistener來設置now.currentTime變量的值。這是我目前的代碼(沒有它返回錯誤,但實際上並沒有工作)。html5視頻事件監聽器沒有得到當前時間

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變量。我也嘗試打印視頻的當前時間,但這也沒有奏效,所以我想我沒有正確地執行eventListener? 謝謝

回答

0

現在越來越計算timeupdate被調用時,而不是調用tran時。所以如果你打電話給tran,那麼現在就是在timeupdate被調用的時候。

爲了獲得準確的,現在變,抓住當前時間在TRAN功能:

function tran(){ 
var video = document.getElementsByTagName('video')[0]; 
now = video.currentTime; 
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 = ""; 
} 
}} 
+0

我明白你說關於它在錯誤的功能是什麼,但後來我不得不對timeupdate調用代替?因爲這仍然沒有聯繫起來。 – Mim 2010-11-28 23:45:36