Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Shortening a code snippet

    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.

    PHP Code:
    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
        {
    "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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    pass in the variable

    function getCurrentTime(timeOnline)

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

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •