Reputation: 767
I'm trying to countdown the duration of an audio file similar to iTunes and I can't get the formula quite right. I searched but can't find what I'm looking for. I'm sure this will be some easy stackoverflow points for someone.
Here's a nearly working example of what I'm working on:
http://jsfiddle.net/philbot/tfSTh/
Basically, if you push play, it will start a timer for the song. Time elapsed works fine, however I'm having trouble with the countdown. I can't get the seconds to countdown correctly. Here's the JS for the countdown portion: (please refer to my fiddle for the rest)
// Countdown
audio.addEventListener("timeupdate", function() {
var timeleft = document.getElementById('timeleft');
var ml = parseInt((audio.duration / 60 - audio.currentTime / 60) % 60);
// Here's the incorrect seconds countdown calc
var sl = parseInt(audio.duration % 60 - audio.currentTime);
if (sl < 10) {
timeleft.innerHTML = ml + ':0' + sl;
}
else {
timeleft.innerHTML = '-' + ml + ':' + sl;
}
}, false);
Thanks Everyone...
Upvotes: 4
Views: 7214
Reputation: 140228
Well I simply just calculated the remaining time and formatted that, is that what you wanted?
jsfiddle: http://jsfiddle.net/tfSTh/1/
Upvotes: 6