Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Stopwatch () {
- var startTime = 0;
- var endTime = 0;
- var running = 0;
- var duration = 0;
- this.start = function() {
- if (running) {
- throw new Error('Stopwatch has alredy started');
- }
- startTime = new Date();
- running = true;
- };
- this.stop = function() {
- if (!running) {
- throw new Error('Stopwatch is not started');
- }
- endTime = new Date();
- running = false;
- duration = endTime.getTime() - startTime.getTime();
- this.duration = duration / 1000;
- };
- this.reset = function() {
- startTime = 0;
- endTime = 0;
- running = 0;
- duration = 0;
- this.duration = 0;
- };
- Object.defineProperty(this, 'duration', {
- get: function() {
- return duration;
- },
- set: function(val) {
- duration = val;
- }
- })
- }
Add Comment
Please, Sign In to add comment