...

View Full Version : Shortening a code snippet



name _F1
11-30-2006, 09:14 AM
I'm trying to simplify my code as much as possible. The function I am writing about involves converting a number of milliseconds into hours, minutes, seconds, milliseconds. This will also be used in some other functions. Is there a way to make this code generic (in an outside function) so it can be reused in various functions?

I've omitted any parts of the function that are irrelevant to my question.


function getCurrentTime() {
mSeconds = timeOnline % 1000;
timeOnline = (timeOnline-mSeconds) / 1000;
seconds = timeOnline % 60;
timeOnline = (timeOnline-seconds) / 60;
minutes = timeOnline % 60;
timeOnline = (timeOnline-minutes) / 60;
hours = timeOnline;
seconds = formatTime(seconds);
minutes = formatTime(minutes);
hours = formatTime(hours);
}

function formatTime(i) {
if (i<10)
{i = "0" + i}
return i
}

As you can see, the formatting of the time is already in an outside function, but I couldn't work out how to change the conversion. Perhaps the outside function could include the formatting of the time, too (it calls the function to format the time, I mean, not repeats that function itself).

Any help would be appreciated. :thumbsup:

A1ien51
11-30-2006, 01:41 PM
pass in the variable

function getCurrentTime(timeOnline)

Than do what ever else you want to do with it.

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum