tags:

views:

70

answers:

1

I am currently using this method to convert milliseconds to min:sec:1/10sec. However it does not seem to be efficient at all. Would anyone know of a faster more efficient and optimized way of accomplishing the same.

mills.prototype.formatTime = function(time) {   
    var elapsedTime = (time * 1000);
    //Minutes
    var elapsedM = (elapsedTime/60000)|0;
    var remaining = elapsedTime - (elapsedM * 60000);
    //add a leading zero if it's a single digit number
    if (elapsedM < 10) {
            elapsedM = "0"+elapsedM;
            }   
    //Seconds
    var elapsedS = ((remaining/1000)|0);
    remaining -= (elapsedS*1000);
        ////add leading zero
        if (elapsedS<10) {
            elapsedS = "0"+elapsedS;
        }
    //Hundredths
    var elapsedFractions = ((remaining/10)|0);  
        if (elapsedFractions < 10) {
            elapsedFractions = "0"+elapsedFractions;
        }
    //display results nicely
    var time_data = elapsedM+":"+elapsedS+":"+elapsedFractions;
    //return time_data;
    return[time_data,elapsedM,elapsedS,elapsedFractions]
    };
+4  A: 

Why not use the date object?

var dateObj=new Date(3500);
var minPart=dateObj.getMinutes();
var secondsPart=dateObj.getSeconds();
var tenthsPart=dateObj.getMilliseconds()/100;

alert(minPart+':'+secondsPart+':'+tenthsPart);

More info.

Gert G
Ahahahaha.... Looks much more efficient than the code posted above..+1
ItzWarty
Yes, this way is certainly smaller. I just wonder if it performs any faster by using the date object and its methods vs. running math routines on raw numbers. Any thoughts on that?
cube
You're correct. It turned out to be slower. An idea for you would be to use setInterval and have the timer function update the time at some regular intervals and you'll just grab the value when dragging. That way you don't need to calculate the time while dragging.
Gert G
That's a great idea, Asynchronous. This wouldn't be taxing on memory would it? btw, Thanks for your help thus far, I really appreciate it.I stumbled across this bit of code which seems to be inline with what I am looking for, although it's for hours:min:sec, but I'm having a hard time trying to convert it to apply to my specific needs which is min:seconds:1/10seconds.The modulus operator has me stuck, do you think it can work for me, if so how would I convert it?seconds=(mills/1000)%60minutes=(mills/(1000*60))%60hours=(mills/(1000*60*60))%24
cube
Not sure if it would be a memory hog or not. Try it out and let us know. :)
Gert G