2
var timer = setInterval(function () {
    var secs = ytplayer.getCurrentTime();
    var minutes = Math.floor(secs / 60);
    var seconds = Math.floor(secs % 60);
    var tsecs = ytplayer.getDuration();
    var tminutes = Math.floor(tsecs / 60);
    var tseconds = Math.floor(tsecs % 60);
    var time = minutes + ":" + seconds + " / " + tminutes + ":" + tseconds;
    document.getElementById('currenttime').innerHTML = time;
}, 1000);

This code does not work in firefox but it does in chrome and IE.

I checked with firebug and it says that the variables secs, minutes, seconds, tsecs, tminutes, tseconds and time are not defined.

I dont know how to fix this because I did use var to define them. Does anyone know what I have done wrong?

Thanks

Joseph
  • 117,725
  • 30
  • 181
  • 234
cohen
  • 621
  • 1
  • 9
  • 21

2 Answers2

2

It sounds like you have an element with the id ytplayer and are assuming that window.ytplayer will be created automatically.

Don't make that assumption. Use document.getElementById (and make sure you have a Doctype that triggers Standards mode).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Actually I have document.getElementById earlier in the code so I thought that It would already be defined. `var ytplayer = document.getElementById("ytplayer");` Putting that in the function makes it work but why do I have to declare it in every function? – cohen May 16 '12 at 10:10
  • Presumably because the [scope](http://stackoverflow.com/questions/500431/javascript-variable-scope) you put it is wasn't wide enough. – Quentin May 16 '12 at 10:16
  • Ok ill look up about scope. Thanks. – cohen May 16 '12 at 10:19
0

try this

var timer = setInterval(function(){
    var secs = ytplayer.getCurrentTime(),
    minutes = Math.floor(secs / 60),
    seconds = Math.floor(secs % 60),
    tsecs = ytplayer.getDuration(),
    tminutes = Math.floor(tsecs / 60),
    tseconds = Math.floor(tsecs % 60),
    time = minutes + ":" + seconds + " / " + tminutes + ":" + tseconds;
    document.getElementById('currenttime').innerHTML = time;
},1000); 

and make sure ytplayer is already there.

mons droid
  • 1,038
  • 9
  • 10