Created
June 30, 2011 20:00
-
-
Save tinyfly/1057075 to your computer and use it in GitHub Desktop.
Calculate the elapsed time between two dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create 1 global variable for namespacing purposes. | |
if (typeof tinyfly === 'undefined') { | |
tinyfly = {}; | |
} | |
/* Handles calculating elapsed time between two dates */ | |
tinyfly.elapsedTime = (function() { | |
var times = {}, | |
timeStart, timeEnd; | |
/* Function: timeStart | |
Parameters: | |
name - string used as a key value to store the date object | |
*/ | |
timeStart = function(name) { | |
times[name] = new Date(); | |
}; | |
/* Function: timeEnd | |
Parameters: | |
name - string used as a key value to lookup the stored startTime. Must match name passed in to timeStart | |
Returns: (number) the time diference in seconds to 1 decimal place | |
*/ | |
timeEnd = function(name) { | |
return Math.round(((new Date() - times[name]) / 1000) * 10 ) / 10; | |
}; | |
return { | |
timeStart: timeStart, | |
timeEnd: timeEnd | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment