1

This is to do with the youtube api and getting the current time.

    var currenttime = ytplayer.getCurrentTime();
    document.getElementById('currenttime').innerHTML = currenttime;

    <p id="currenttime"></p>

How would I make it keep updating the current time so that you can see the time going up on the screen while the video is playing?

cohen
  • 621
  • 1
  • 9
  • 21

1 Answers1

4

You could use a timer that will fetch the time every one second.

var timer = setInterval(function(){
          document.getElementById("currenttime").innerHTML = currenttime;
},1000);

Note:
Take a closer look (on the difference between setInterval and setTimeout): setTimeout or setInterval?

Community
  • 1
  • 1
funerr
  • 7,212
  • 14
  • 81
  • 129
  • cheers thats perfect. Now its displays to 3 decimal places though. Do you know how to limit it to whole digits? – cohen May 15 '12 at 12:26
  • and it displays all in seconds as well. Would I just divide by 60 then do inner html? – cohen May 15 '12 at 12:27
  • For the limit of the number of digits I think you can use the substring function: currenttime.substring(0,desired-length); And for the seconds, yes just divide by 60. – funerr May 15 '12 at 12:29
  • I really appreciate the help mate! Your awesome :) – cohen May 15 '12 at 12:30
  • one problem with dividing by 60 when it goes to .100 before going to 1.0 How would you make it into actual minutes and seconds? DW I found another question on here for that. http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss – cohen May 15 '12 at 12:47
  • I didn't really understand your problem, do you mean that you don't want any digits after the full stop? Or do you want to convert the time unit? (https://www.google.com/search?rlz=1C1AVSC_enIL436IL436&sourceid=chrome&ie=UTF-8&q=js+convert+seconds+to+mintues#hl=en&rlz=1C1AVSC_enIL436IL436&sa=X&ei=RVGyT9yPEOek0QXm4oSKCQ&ved=0CBoQvwUoAQ&q=js+convert+seconds+to+minutes&spell=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=356e729b7a15c7f0&biw=1680&bih=925) – funerr May 15 '12 at 12:51