Skip to content

Instantly share code, notes, and snippets.

@rushfrisby
Last active April 7, 2022 13:28
Show Gist options
  • Save rushfrisby/3c0dc10187ee4da04e7f to your computer and use it in GitHub Desktop.
Save rushfrisby/3c0dc10187ee4da04e7f to your computer and use it in GitHub Desktop.
Stopwatch class in JavaScript
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();
}
}
@k-tten
Copy link

k-tten commented Jan 11, 2021

thx im gonna take it and rewrite it with es6 classes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment