Pollux Khafra
Pollux Khafra

Reputation: 862

Show live current elapsed time of a youtube video

I know how to get the current elapsed time of a youtube video using...

player.getCurrentTime()

But I need a way to show this as updating text inside an element so I can display the current time of the youtube video counting while the video is playing. What's the correct way to do this preferably using jquery?

Upvotes: 1

Views: 5842

Answers (2)

XefNed
XefNed

Reputation: 17

Here's a working code playground example that should help:

https://code.google.com/apis/ajax/playground/#polling_the_player

Upvotes: 0

John Dvorak
John Dvorak

Reputation: 27317

If the player doesn't offer a callback when the seconds roll over, you could still poll the current time. Choosing the correct polling frequency is a performance trade-off, but you don't really need to crank it up:

setInterval(function(){
   display.innerText = player.getCurrentTime();
},100); //polling frequency in miliseconds

jQuery does not really simplify the waiting but you can use it for DOM manipulation:

setInterval(function(){
   $display.text(player.getCurrentTime());
},100); //polling frequency in miliseconds

Upvotes: 4

Related Questions