Last active
April 7, 2022 13:28
-
-
Save rushfrisby/3c0dc10187ee4da04e7f to your computer and use it in GitHub Desktop.
Stopwatch class in JavaScript
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
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
thx im gonna take it and rewrite it with es6 classes :)