2014-01-10 53 views
0

我寫了這樣的代碼,播放按鈕的作品,但計時器沒有。如何使用JavaScript在視頻上創建計時器?

<input type="button" id="play" onclick="videoplay();" value=">"/> 

....

<span id="timer">0%</span> 

....

function videoplay() { 
     var video = document.getElementById("video"); 
     var button = document.getElementById("play"); 
     if (video.paused) { 
      video.play(); 
      button.value = "||"; 
      var timer = getElementbyId("timer"); 
      var update = setInterval(function() { 
       timer.textContent = Math.round(video.currentTime/video.duration * 100) + "%"; 
      }, 500); 
     } else { 
      video.pause(); 
      button.value = ">"; 
      if (update) 
       clearInterval(update); 
     } 
    } 

回答

0
timer.innerText = Math.round(video.currentTime/video.duration * 100) + "%"; 
+0

的innerText?怎麼樣更多的Firefox和更少的IE瀏覽器? – dandavis

+0

應該使用jQuery,所以我們不必翻轉。 –

+0

它完全值得100kb的代碼,以便您可以使用「.html()」而不是「.innerHTML」... – dandavis