There is more to exporting the conversion than just simplifying the code.
I would assume that your project will be more than this one function, and that you will more
often need to make time conversions... if that is the case I would like to think you wouldn't
want to add time conversion to every function...
Code:
window.Project = function () {
/*I like to keep all my privates together*/
var HHMMSS = function(secs){ // Now we can just plug this in anywhere we need it
var time = [0 , 0 , secs];
for (var i = 2 ; i>0 ; i--) {
time[i-1] = Math.floor(time[i] / 60);
time[i] = time[i] % 60;
if (time[i] < 10)
time[i] = '0' + time[i];
}
return time; // We can return this as an array to allow for greater flexibility, we can always .join(':') in the calling function...
};
/*I like to keep all my publics together*/
var CnvToTime = function(FrameCount) {
var FRate = 29.97 , // This just keeps things looking clean by assigning all of the following as local via the same var declaration (up until the ";")
TotalSecs = parseInt((FrameCount / FRate) , 10) ,
ElapsedTime = HHMMSS(TotalSecs) ,
frames = (FrameCount - TotalSecs * 30) , // frames and dropframes are only ever used in the expression (frames + dropframes). If you have not intention of changing that you could remove a 2 lines of code with FramesDropped = (FrameCount - TotalSecs * 30) + ((ElapsedTime[0] * 108) + (ElapsedTime[1] * 2) - (parseInt((ElapsedTime[1] / 10) * 2), 10))
dropframes = (ElapsedTime[0] * 108) + (ElapsedTime[1] * 2) - (parseInt((ElapsedTime[1] / 10) * 2), 10) ,
timecode = '' , // I don't see the wisdom of making this an array when you convert it into a string later...
FramesDropped = frames + dropframes; /* A little "micro-optimization" technique to keep in mind (totally optional) - when you find
yourself re-evaluating the same expression many times you could shrink your code and execute
faster (if this is run like 10000+ times per second...) if you save your evaluated expression
to a var and reference that. Admitedly it is trival in this case, just a little food for thought ;)*/
if (FramesDropped > 30) {
ElapsedTime[2] += 1;
timecode = ElapsedTime.join(':') + ":" + "01";
}
else if (FramesDropped == 30) {
ElapsedTime[2] += 1;
timecode = ElapsedTime.join(':') + ":" + "00";
}
else {
timecode = ElapsedTime.join(':') + ":" + FramesDropped;
}
return timecode;
};
return {
'getDroppedFrames' : CnvToTime // you can use a different alias for calling CnvToTime if you want
};
}();
document.write( Project.getDroppedFrames(3000) );
taking into account some of my suggestions above (plus one more thing I just thought of) you might get the following...
Code:
window.Project = function () {
var HHMMSS = function(secs) {
var time = [0 , 0 , secs];
for (var i = 2 ; i>0 ; --i) {
time[i-1] = Math.floor(time[i] / 60);
time[i] = time[i] % 60;
if (time[i] < 10)
time[i] = '0' + time[i];
}
return time;
};
var CnvToTime = function(FrameCount) {
var FRate = 29.97 ,
TotalSecs = parseInt((FrameCount / FRate) , 10) ,
ElapsedTime = HHMMSS(TotalSecs) ,
FramesDropped = (FrameCount - TotalSecs * 30) + ((ElapsedTime[0] * 108) + (ElapsedTime[1] * 2) - (parseInt((ElapsedTime[1] / 10) * 2), 10));
if (FramesDropped > 30) {
ElapsedTime[2] += 1;
FramesDropped = ':01';
}
else if (FramesDropped == 30) {
ElapsedTime[2] += 1;
FramesDropped = ':00'; // <--oops I goofed that string again, there, all better now
}
return ElapsedTime.join(':') + FramesDropped;
};
return {
'getDroppedFrames' : CnvToTime
};
}();
document.write( Project.getDroppedFrames(3000) );
each function acts independant of eachother, and to me that's just cleaner, I know that HHMMSS will always
give me the right return, and if I have to make changes to CnvToTime later I know I won't accidentally screw up
my seconds conversion
I just hope I followed your algorithm properly (i noticed that my last post skipped over the fact that
you needed the isolated values for hour and min

), you should double check that it gives you the right output...