1

I'm using html5 audio tag in my project. everything works fine.

I need to use the timeupdate and get the value of the minutes that has passed in this format: 00:00

Currently I can only get that value as 0:00 with my code:

This is my code:

$(audio).bind('timeupdate', function(){
    var percent = audio.currentTime/ audio.duration * 100;

    /////Passed time////
    var mins = Math.floor(audio.currentTime / 60);
    var secs = Math.floor(audio.currentTime % 60);
    if (secs < 10) {
        secs = '0' + String(secs);
    }

    $('.Audio_passedTime').text(mins + ':' + secs);
});

And a working fiddle: http://jsfiddle.net/8cpwv2mf/

Could someone please advice on this issue?

Thanks in advance.

executable
  • 3,365
  • 6
  • 24
  • 52
James Juanjie
  • 219
  • 3
  • 18

2 Answers2

1

You could use the same structure you used for the mins, like so:

$(audio).bind('timeupdate', function() {
    var percent = audio.currentTime/ audio.duration * 100;

 /////Passed time////
    var mins = Math.floor(audio.currentTime / 60);
    if (mins < 10) {
      mins = '0' + String(mins);
    }
    var secs = Math.floor(audio.currentTime % 60);
    if (secs < 10) {
      secs = '0' + String(secs);
    }

$('.Audio_passedTime').text(mins + ':' + secs);
Teun van der Wijst
  • 939
  • 1
  • 7
  • 21
1

You can define a function that pads numbers with 0's (like this)

function zpad(n) {
  return ("0" + n).slice(-2);
}

and then use it to format both minutes and seconds

$('.Audio_passedTime').text(zpad(mins) + ':' + zpad(secs));
user2314737
  • 27,088
  • 20
  • 102
  • 114