As part of my project, a game, I have progress bars with countdown timers. These are updated every 250ms, to keep a relatively smooth animation if the countdown time is short.
Here's my function:
var time_format = function(time) {
var elms, names, ret, i, n;
elms = [
time / 29030400,
time / 2419200 % 12,
time / 604800 % 4,
time / 86400 % 7,
time / 3600 % 24,
time / 60 % 60,
time % 60
];
names = "yr mo w d h m s".split(" ");
ret = [];
for( i=0; i<elms.length; i++) {
n = Math.floor(elms[i]);
if( n) ret.push(n+names[i]);
}
ret = ret.join(" ");
if( ret == "") ret = "0s";
return ret;
};
I'm just wondering if any optimisations can be made to this code. There may be up to ten progress bars on a single page (after that data is paginated). My main concern is that Math.floor call, is there a faster way to floor a number?