Skip to content

Instantly share code, notes, and snippets.

@modulexcite
Forked from rushfrisby/stopwatch.js
Last active August 29, 2015 14:23
Show Gist options
  • Save modulexcite/7d1fa279d5aa53cab82d to your computer and use it in GitHub Desktop.
Save modulexcite/7d1fa279d5aa53cab82d to your computer and use it in GitHub Desktop.
function Stopwatch()
{
var sw = this;
var start = null;
var stop = null;
var isRunning = false;
sw.__defineGetter__("ElapsedMilliseconds", function()
{
return (isRunning ? new Date() : stop) - start;
});
sw.__defineGetter__("IsRunning", function()
{
return isRunning;
});
sw.Start = function()
{
if(isRunning)
return;
start = new Date();
stop = null;
isRunning = true;
}
sw.Stop = function()
{
if(!isRunning)
return;
stop = new Date();
isRunning = false;
}
sw.Reset = function()
{
start = isRunning ? new Date() : null;
stop = null;
}
sw.Restart = function()
{
isRunning = true;
sw.Reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment